我試圖構建一個小游戲,隨機生成一個 10、20、50、200、1000 的陣列。它計算一定數量的對,但是它以某種奇怪的方式計算。如果我有 10 個中的 3 個,則計算為 2 對,如果我有 10 個中的 4 個,則計算為 5 對。基本上,我只需要如果它是 10 個中的 4 個,那么它應該有 2 對,如果它是 10 個中的 6 個,則它應該有 3 對。任何提示,建議表示贊賞
int[] bonusGame = new int[5]; // bonus game array
int bonusGameSum=0; // amount of prize for bonus game
int randomChance = r.nextInt(100); // generating chance
for (int newNum = 0; newNum<5 ;newNum ) { // getting chances for numbers
int chanceGen = r.nextInt(100); // generating chance
if (chanceGen <= 50) { // generating chance for 10
bonusGame[newNum] = 10;
}
else if (chanceGen <= 77) { // generating chance for 20
bonusGame[newNum] = 20;
}
else if (chanceGen <= 92) { // generating chance for 50
bonusGame[newNum] = 50;
}
else if (chanceGen <= 98) { // generating chance for 200
bonusGame[newNum] = 200;
}
else { // generating chance for 1000
bonusGame[newNum] = 1000;
}
}
for (int z: bonusGame) {
System.out.println(z);
}
for (int f = 0; f < bonusGame.length - 1; f) { // checking for pairs in array and adding up to winning sum
for (int j = f 1; j < bonusGame.length; j) {
if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 10 ) { // if 10 has pairs
System.out.println(" You won 10 euro ");
bonusGameSum =10;
}
else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 20 ) { // if 20 has pairs
System.out.println(" You won 20 euro ");
bonusGameSum =20;
}
else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 50 ) { // if 50 has pairs
System.out.println(" You won 50 euro ");
bonusGameSum =50;
}
else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 200 ) { // if 200 has pairs
System.out.println(" You won 200 euro ");
bonusGameSum =200;
}
else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 1000 ) { // if 1000 has pairs
System.out.println(" You won 1000 euro ");
bonusGameSum =1000;
}
}
}
uj5u.com熱心網友回復:
您的程式會計算相同值在串列中出現的次數。
為了實作您的意圖,您可以嘗試不同的方法,例如:
1 創建已使用號碼串列
2 使用“陣列串列”洗掉已使用的值或將它們更改為 0
3 創建每個結果的出現串列
我在下面介紹方法 3:
Random r = new Random();
int[] bonusGame = new int[5]; // bonus game array
for (int newNum = 0; newNum < bonusGame.length; newNum )
{ // getting chances for numbers
int chanceGen = r.nextInt(100); // generating chance
if (chanceGen <= 50)
{ // generating chance for 10
bonusGame[newNum] = 10;
}
else if (chanceGen <= 77)
{ // generating chance for 20
bonusGame[newNum] = 20;
}
else if (chanceGen <= 92)
{ // generating chance for 50
bonusGame[newNum] = 50;
}
else if (chanceGen <= 98)
{ // generating chance for 200
bonusGame[newNum] = 200;
}
else
{ // generating chance for 1000
bonusGame[newNum] = 1000;
}
}
for (int z: bonusGame)
{
System.out.println(z);
}
int[] pool = {10, 20, 50, 200, 1000}; // possible winnings
int[] pairs = {0, 0, 0, 0, 0}; // array counting how many times we scored each number
for (int value : bonusGame) // search through random results
{
int index = 0; // matching random result with correct element of the pool
for (int size : pool) // comparing each result with our possible winnings
{
if (value == size)
{
pairs[index] ; // increase number of successes by one
break;
}
index ;
}
}
int index = 0;
for (int value : pairs) // search through created list of values for each random result
{
while (value >= 2) // when such result occurred at least twice
{
value -= 2;
System.out.println("You won " pool[index] " euro");
}
index ;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/364820.html
下一篇:比較兩個多字文本字串的陣列公式
