我正在創建一個老虎機遊戲,如果所有三個數字匹配一條消息「累積獎勵」被傳達。現在,當只有兩個數字與「關閉」匹配時,我想更改此消息,但我正在努力尋找一種方法來測試三個數字中的兩個匹配,並且不會顯示兩個消息。如何檢查在Java中匹配的隨機數
import java.util.Random;
public class SlotMachine {
public static void main(String[] args) {
boolean playing = true;
Random rand = new Random();
int number1 = rand.nextInt(2)+1;
int number2 = rand.nextInt(2)+1;
int number3 = rand.nextInt(2)+1;
int total = number1 + number2 + number3;
System.out.println("-------------------");
System.out.println("| " + "("+ number1 + ")" + "(" + number2 + ")" + "(" + number3 + ")" + " |");
System.out.println("-------------------");
if(number1*3 == total && number2*3 == total && number3*3 == total){
System.out.println("| JACKPOT!!! |");
System.out.println("-------------------");
playing = false;
}
if(number1 == number2) {
System.out.println("| CLOSE.. |");
System.out.println("-------------------");
}
}
}
認真嗎?只要用盡'if's並檢查3個數字中的2個組合是否匹配..只有'6 C 2 = 15'選項... – Idos
僅僅使用'numberX = = numberY'而不是計算'total'並執行乘法以找到相等的數字?這同樣適用於你的「關閉」搜索 – Tom
,因爲你已經在爲'jackpot'做3次檢查,爲什麼不簡單地'n1 == n2 && n2 == n3 && n1 == n3'? – AxelH