双色球的程序代码
雙色球其實(shí)是有個(gè)很有趣的小游戲,僅供大家參考和學(xué)習(xí),別無(wú)他意.
代碼:
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個(gè)和一個(gè)藍(lán)色球:[1,16]Random rd = new Random();//隨機(jī)生成6個(gè)各不相同的數(shù),對(duì)其升序排序int[] comRed = new int[6];//隨機(jī)生成一個(gè)藍(lán)色球int comBlue = rd.nextInt(16) + 1;//循環(huán)生成6個(gè)隨機(jī)數(shù)for (int i = 0; i < 6; i++) {//隨機(jī)生成6個(gè)紅色球comRed[i] = rd.nextInt(33) + 1;//新生成的數(shù)的索引i 已生成的數(shù)的索引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++) {//如果新生成的數(shù)和已生成的數(shù)相同,重新生成隨機(jī)數(shù)if (comRed[i] == comRed[j]) {i--;break;}}}//排序Arrays.sort(comRed);/** 購(gòu)買彩票* */Scanner sc = new Scanner(System.in);//定義數(shù)組和變量保存用戶選擇的號(hào)碼int[] userRed = new int[6];int userBlue;//循環(huán)接6個(gè)數(shù)for (int i = 0; i < 6; i++) {System.out.println("請(qǐng)輸入第" + (i + 1) + "個(gè)紅色球號(hào)碼");int inp = sc.nextInt();//輸入有誤時(shí),重新輸入,不再執(zhí)行后續(xù)代碼if (inp < 1 || inp > 33) {System.out.println("輸入有誤");i--;continue;}//保存到數(shù)組中userRed[i] = inp;//不重復(fù)for (int j = 0; j < i; j++) {if (userRed[i] == userRed[j]) {System.out.println("該號(hào)碼已選擇");i--;}}}//對(duì)用戶輸入的紅色號(hào)碼排序Arrays.sort(userRed);//接收輸入的藍(lán)色球號(hào)碼int inp;do {//正確的藍(lán)色球System.out.println("請(qǐng)輸入藍(lán)色球號(hào)碼:");inp = sc.nextInt();if (inp < 1 || inp > 16) {System.out.println("輸入有誤");}userBlue = inp;} while (inp < 1 || inp > 16);/*打印結(jié)果*///打印用戶的紅色球System.out.println("你購(gòu)買的紅色球:");for (int i : userRed) {System.out.print(i + "\t");}System.out.println();//打印用戶上的藍(lán)色球System.out.println("你購(gòu)買的藍(lán)色球:");System.out.println(userBlue);//打印電腦的紅色球System.out.println("紅色球開(kāi)獎(jiǎng)結(jié)果:");for (int i : comRed) {System.out.print(i + "\t");//程序休眠,1000ms表示1sThread.sleep(1000);}System.out.println();//打印電腦的藍(lán)色球System.out.println("藍(lán)色球開(kāi)獎(jiǎng)結(jié)果:");System.out.println(comBlue);}
}
結(jié)果
?
總結(jié)
- 上一篇: 开机自启动脚本_使用xtu降低笔记本(游
- 下一篇: miui12怎么自定义开机动画_MIUI