import java.util.*;
class token {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
Random number = new Random();
System.out.println("Do you want to play again(y/n)? ");
String in = input.nextLine();
do{
int num1,num2,sum;
num1 = 1+number.nextInt(6);
num2 = 1+number.nextInt(6);
sum = num1 + num2;
System.out.print("Dice 1: "+num1);
System.out.print("\nDice 2: "+num2);
System.out.print("\nSum: "+sum);
if(sum%2==1){
System.out.println("\nYou have lost 1 token.");
}else if(sum>10){
System.out.println("\nYou have won 2 tokens.");
}else if(sum>11){
System.out.println("\nYou have won 2 tokens.");
}else if(sum>12){
System.out.println("\nYou have won 2 tokens.");
}else{
System.out.println("\nYou have won 1 token.");
}
System.out.println("Do you want to play again(y/n)? ");
String in1 = input.nextLine();
}while(in == "y");
}
}
所以我嘗試使用do while循環,但不幸的是,它不會循環。 樣品輸出: 您想再次玩(y/n)嗎? y 骰子1:3 骰子2:4 總和:7 您已經丟失了1個標記。 你想再次玩(y/n)嗎? y如何把這個程序放入java的循環中?
我希望我的程序不斷循環,除非用戶輸入n。
實際上,他也應該用'in = input.nextLine();'替換'in1 = input.nextLine();'。 –