我的程序无效!我需要它随机滚动两个骰子,计算卷数,直到达到蛇眼(2个) 我觉得我很亲密,但是我的程序只会滚动一次,而不是很多次,直到滚动两次。
我得到的输出被计为一个骰子卷的无限循环!
import java.io.*;
public class PA
{
public static void main (String [] args)
{
int count = 0;
int die1 = (int)(Math.random()*6)+1;
int die2 = (int)(Math.random()*6)+1;
int sumDie = die1+die2;
do
{
System.out.println ("You rolled a:" + die1);
System.out.println ("You rolled a:" + die2);
System.out.println ("This is roll number:" + count);
count = count + 1;
}
while (sumDie > 2);
{
System.out.println ("You rolled:" + die1);
System.out.println ("You rolled:" + die2);
System.out.println ("This is roll number:" + count):
count++
}
if (die1 == 1 && die2 == 1);
System.out.println ("Congratulations...Snake Eyes");
}
}
答案 0 :(得分:1)
您需要将掷骰子的代码移动到循环内部。
并且,不需要那段时间后的代码块。
最后if
- 首先它因为分号而无效(没有身体)。其次,不需要,因为while
条件确保了这种情况。
public static void main (String [] args)
{
int count = 0;
int die1, die2;
do
{
// New roll each time
die1 = (int)(Math.random()*6)+1;
die2 = (int)(Math.random()*6)+1;
count = count + 1;
System.out.println ("You rolled a:" + die1);
System.out.println ("You rolled a:" + die2);
System.out.println ("This is roll number:" + count);
} while (die1 + die2 > 2);
System.out.println ("Congratulations...Snake Eyes");
}