我正在尝试为骰子游戏编写一个程序,在游戏中掷三个骰子。每个“骰子”的侧面都有从“1”到“6”的数字。如果数字都是 6,则用户获得 500 积分。如果两个数字匹配,但不是三个,则用户获得 50 分。如果用户有两个 6(但不是三个 6),则他/她获得 100 分。如果没有数字匹配,则用户将失去一分。
到目前为止我有这个:
import java.util.Random;
public class diceGame {
public static void main (String args[])
{
Random randomGenerator = new Random ();
int a;//first face of die
a = randomGenerator.nextInt((5)+1);
System.out.println(a);
int b;//second face of die
b = randomGenerator.nextInt((5)+1);
Random randomGenerator2 = new Random ();
System.out.println(b);
int c;//third face of die
c = randomGenerator.nextInt((5)+1);
Random randomGenerator3 = new Random ();
System.out.println(c);
...
}
...
}
但是一旦我进入嵌套的 if 语句,我就会陷入困境。
最佳答案
if (a == 6 && b == 6 && c == 6){
//Add to score
}
else if ((a == b == 6 && b != c) || (b == c == 6 && c !=a)){
//Add to score
}
else if ((a == b && b != c) || (b == c && c !=a)){
//Add to score
}
//...
关于java - 用 Java 模拟骰子游戏程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31326807/