2 回答

TA貢獻1909條經驗 獲得超7個贊
您的代碼看起來很適合與Scanner. 我會再看看你的循環(不清楚內部do...while()循環在做什么)以及你的其他代碼中還有什么(什么是Prediction?你的設置方式是否存在錯誤valid?)。
這是一個非常簡單的循環,Scanner永遠循環,詢問一個名字,打印這個名字:
Scanner scanner = new Scanner(System.in);
do {
System.out.println("input your name");
String name = scanner.nextLine();
System.out.println("name: " + name);
} while (true);

TA貢獻1818條經驗 獲得超8個贊
do{
int n = rand.nextInt(50);
int m = rand.nextInt(50);
int o = rand.nextInt(50);
System.out.println("blah blah fortune");
System.out.println("input your name"); // This part doesn't work the second time
String name = scanner.next();
System.out.println();
Prediction(name,n,m,o);
System.out.println();
System.out.println("do you wish to guess another fortune?");
System.out.println("1 is yes other number no");
//this part I will omit tests if the answer is valid and if it should repeat
itself, it works for now.
}
while (repeat==true);
使用 scanner.next() 而不是 nextLine(),nextLine() 將在下次開始時將空行作為輸入,因此使用 next()。這就是為什么它不接受輸入的原因。您可以只用一個 while 循環實現相同的功能,但不確定您的要求是什么。
添加回答
舉報