双色球的程序代码
雙色球其實是有個很有趣的小游戲,僅供大家參考和學習,別無他意.
代碼:
package test;import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;public class ShuangSeQiu {public static void main(String[] args) throws InterruptedException {//彩票雙色球程序//一組紅色球:[1,33]選6個和一個藍色球:[1,16]Random rd = new Random();//隨機生成6個各不相同的數,對其升序排序int[] comRed = new int[6];//隨機生成一個藍色球int comBlue = rd.nextInt(16) + 1;//循環生成6個隨機數for (int i = 0; i < 6; i++) {//隨機生成6個紅色球comRed[i] = rd.nextInt(33) + 1;//新生成的數的索引i 已生成的數的索引j//1 0//2 0 1//3 0 1 2//4 0 1 2 3//5 0 1 2 3 4//6 0 1 2 3 4 5//i [0,i)for (int j = 0; j < i; j++) {//如果新生成的數和已生成的數相同,重新生成隨機數if (comRed[i] == comRed[j]) {i--;break;}}}//排序Arrays.sort(comRed);/** 購買彩票* */Scanner sc = new Scanner(System.in);//定義數組和變量保存用戶選擇的號碼int[] userRed = new int[6];int userBlue;//循環接6個數for (int i = 0; i < 6; i++) {System.out.println("請輸入第" + (i + 1) + "個紅色球號碼");int inp = sc.nextInt();//輸入有誤時,重新輸入,不再執行后續代碼if (inp < 1 || inp > 33) {System.out.println("輸入有誤");i--;continue;}//保存到數組中userRed[i] = inp;//不重復for (int j = 0; j < i; j++) {if (userRed[i] == userRed[j]) {System.out.println("該號碼已選擇");i--;}}}//對用戶輸入的紅色號碼排序Arrays.sort(userRed);//接收輸入的藍色球號碼int inp;do {//正確的藍色球System.out.println("請輸入藍色球號碼:");inp = sc.nextInt();if (inp < 1 || inp > 16) {System.out.println("輸入有誤");}userBlue = inp;} while (inp < 1 || inp > 16);/*打印結果*///打印用戶的紅色球System.out.println("你購買的紅色球:");for (int i : userRed) {System.out.print(i + "\t");}System.out.println();//打印用戶上的藍色球System.out.println("你購買的藍色球:");System.out.println(userBlue);//打印電腦的紅色球System.out.println("紅色球開獎結果:");for (int i : comRed) {System.out.print(i + "\t");//程序休眠,1000ms表示1sThread.sleep(1000);}System.out.println();//打印電腦的藍色球System.out.println("藍色球開獎結果:");System.out.println(comBlue);}
}
結果
?
總結
- 上一篇: 开机自启动脚本_使用xtu降低笔记本(游
- 下一篇: miui12怎么自定义开机动画_MIUI