【发布时间】:2021-11-23 19:35:11
【问题描述】:
我尝试构建一个小游戏,随机生成数组 10,20,50,200,1000。它计算成对的数量,但是它以某种奇怪的方式计算。如果我有 10 个中的 3 个 - 它被计算为 2 对,如果我有 10 个中的 4 个,它被计算为 5 对。基本上,我只需要 4 of 10 那么它应该有 2 双,如果它是 6 of 10 它应该有 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;
}
}
}
【问题讨论】:
-
至少对我来说,你不清楚你在哪里为“对”计算编码。
-
请提供预期输出与实际输出的示例。我不了解您的代码的目标或您要实现的目标