導航:首頁 > 編程語言 > java隨機抽取

java隨機抽取

發布時間:2023-07-11 01:51:48

A. 請問用java從1-33個整數中隨機抽取6個數字 且不重復 1-16隨機抽取一個數,給小球

完整代碼為:

public class Main {

public static void main(String[] args) {
int index = 1;
int[] redBalls = new int[6];

Random random = new Random();
boolean getMoreRed = true;
boolean getAgain;
System.out.println("開始抽取紅球!");
while (getMoreRed) {
getAgain = false;
int red = random.nextInt(36) + 1;
System.out.print("本次抽取到的紅球為:[" + red + "]!");
for (int i = 0; i < index; i++) {
if (redBalls[i] == red) {
System.out.print("重復抽取,將重新抽取紅球");
getAgain = true;
break;
}
}
System.out.println("");
if (getAgain){
continue;
}
redBalls[index - 1] = red;
index++;
getMoreRed = index < 7;
}
System.out.println("抽取到的紅球為:");
Arrays.sort(redBalls);
for (int redBall : redBalls) {
System.out.print(redBall + " ");
}

System.out.println(" 開始抽取藍球!");
System.out.println("本次抽取到的藍球為:[" + (random.nextInt(16) + 1) + "]!");
}
}

閱讀全文

與java隨機抽取相關的資料

熱點內容
單片機串列通信方式 瀏覽:487
android表格demo 瀏覽:265
安卓怎麼讓相冊不顯示網頁 瀏覽:302
php文件系統源碼 瀏覽:715
易信java 瀏覽:488
北侖二級壓縮螺桿機 瀏覽:51
加密路線免費入口 瀏覽:746
計算器程序員是幹嘛的 瀏覽:363
數控編程里fo是啥鍵 瀏覽:662
javacdataxml 瀏覽:951
本機tcp伺服器地址 瀏覽:434
切換pdf 瀏覽:99
iturns下載的app在哪裡 瀏覽:648
雲起書院App在哪裡 瀏覽:622
怎麼對hex文件進行加密 瀏覽:293
pdf少年時 瀏覽:962
兩個同名文件夾同步 瀏覽:638
拍照文件夾哪個好用 瀏覽:98
文件夾和書包 瀏覽:518
解壓歸檔拿登記證 瀏覽:232