我正在尝试建立一个掷骰子游戏,其中如果计算机自动掷一对骰子,如果cpu掷出7或11,用户获胜。但是,如果用户掷出2、3或12,他们会自动输掉。如果用户滚动任何其他数字(4,5,6,8,9,10),这是“点”,他们将尝试再次滚动该点。(除非他们掷出7,否则他们会输。)我正在尝试让我的while循环继续滚动,如果计算机滚动的数字不是7或"point“,唯一运行的是”恭喜你滚动了7你赢了“或”你已经滚动了3你所爱的“。
这就是我现在拥有的..
public class CrapsPractice {
public static void main(String[] args) {
int d1 = (int) (6 * Math.random() + 1);
int d2 = (int) (6 * Math.random() + 1);
int roll = (d1 + d2);
int point = roll;
if (roll == 7 || roll == 11)
{
System.out.println("You rolled a" + roll);
System.out.println("Congrats! You've immediately won!");
}
else if (roll == 2 || roll == 3 || roll == 12) {
System.out.println("you rolled a " + roll);
System.out.println("You lose!");
while (roll != 7 && roll != point) {
int d3 = (int) (6 * Math.random() + 1);
int d4 = (int) (6 * Math.random() + 1);
roll = d3 + d4;
System.out.println("your point is" + point);
}
}
}
}发布于 2019-09-23 23:44:28
您的花括号放错了位置。在while启动后,您正在关闭您的else if。您应该将格式重新设置为如下所示:
public class crapsPractice{
public static void main(String[]args){
int d1 = (int) (6 * Math.random() + 1);
int d2 = (int) (6 * Math.random() + 1);
int roll = (d1 + d2);
int point = roll;
if (roll == 7 || roll == 11)
{
System.out.println("You rolled a" + roll);
System.out.println("Congrats! You've immediately won!");
}
else if (roll == 2 || roll == 3 || roll == 12)
{
System.out.println("you rolled a " + roll);
System.out.println("You lose!");
}
while(roll != 7 && roll != point )
{
int d3 = (int) (6 * Math.random() + 1);
int d4 = (int) (6 * Math.random() + 1);
roll = d3 + d4;
System.out.println("your point is" + point);
}
}
} 发布于 2019-09-23 23:46:15
NotZack是对的,检查你的括号,但你也有这个:
while(roll != 7 && roll != point )
roll和point不是相同的吗,所以它永远不会运行你的while循环
发布于 2019-09-23 23:52:45
您忘记添加else部分,这就是为什么不打印其他数字的原因。此外,类名始终以大写字母开头。
public class CrapsPractice {
public static void main(String[] args) {
int d1 = (int) (6 * Math.random() + 1);
int d2 = (int) (6 * Math.random() + 1);
int roll = (d1 + d2);
int point = roll;
if (roll == 7 || roll == 11)
{
System.out.println("You rolled a" + roll);
System.out.println("Congrats! You've immediately won!");
}
else if (roll == 2 || roll == 3 || roll == 12) {
System.out.println("you rolled a " + roll);
System.out.println("You lose!");
while (roll != 7 && roll != point) {
int d3 = (int) (6 * Math.random() + 1);
int d4 = (int) (6 * Math.random() + 1);
roll = d3 + d4;
System.out.println("your point is" + point);
}
}
else
{
System.out.println("your point is " + point);
}
}
}https://stackoverflow.com/questions/58065787
复制相似问题