如何结束这个循环
问题描述:
public class Prueba {
public static void main (String [] args) {
int dados1, dados2, dados3, dados4, puntuacion, x;
Random aleatorio = new Random();
dados1 = aleatorio.nextInt(6);
dados2 = aleatorio.nextInt(6);
dados3 = aleatorio.nextInt(6);
dados4 = aleatorio.nextInt(6);
System.out.println("El primer jugador obtiene una tirada de "
+ (dados1+1)+" "+(dados2+1));
System.out.println("El segundo jugador obtiene una tirada de "
+ (dados3+1)+" "+(dados4+1));
while (dados1==dados2) {
puntuacion = (dados1+dados2+20);
System.out.println("La puntuacion del primer jugador es " + puntuacion);
}
}
}
这是一个模拟滚动两个骰子的游戏。每个人得到两个骰子,并且得分像如何结束这个循环
puntuacion = (dados1+dados2+20);
我该如何结束这个循环并打印得分?如果我打印出来的while
循环不承认puntuacion
答
如果dado1相同dado2然后你把自己锁在while循环中...... 您需要重新计算这些值,而内或土长“while”为另一个逻辑评估者如“if”
答
您可能想尝试这样的事情。 因此,您每次计算dados1和dados2,并将它们进行比较,如果它们匹配,则会打印puntuacion并跳出循环。
public class Prueba{
public static void main(final String[] args){
int dados1, dados2, dados3, dados4, puntuacion;
final int x;
final Random aleatorio = new Random();
while (true){
dados1 = aleatorio.nextInt(6);
dados2 = aleatorio.nextInt(6);
dados3 = aleatorio.nextInt(6);
dados4 = aleatorio.nextInt(6);
System.out.println("El primer jugador obtiene una tirada de " + (dados1 + 1) + " " + (dados2 + 1));
System.out.println("El segundo jugador obtiene una tirada de " + (dados3 + 1) + " " + (dados4 + 1));
if (dados1 == dados2)
{
puntuacion = dados1 + dados2 + 20;
System.out.println("La puntuacion del primer jugador es " + puntuacion);
break;
}
}
}
}
if语句,你应该使用的,像'如果(dado1 == dados2)' – mmking
您需要更改dados1'的'价值观和'dados2' * *里面你'while'循环。 – rossum
你需要更具体地了解程序应该做什么 –