這裏是我的代碼:如果用戶輸入Y,如何保持程序運行?
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner Keyboard = new Scanner(System.in);
{
System.out.println("What is the answer to the following problem?");
Generator randomNum = new Generator();
int first = randomNum.num1();
int second = randomNum.num2();
int result = first + second;
System.out.println(first + " + " + second + " =");
int total = Keyboard.nextInt();
if (result != total) {
System.out.println("Sorry, wrong answer. The correct answer is " + result);
System.out.print("DO you to continue y/n: ");
} else {
System.out.println("That is correct!");
System.out.print("DO you to continue y/n: ");
}
}
}
}
我試圖讓程序繼續運行,但如果用戶輸入y和如果他進入ñ關閉。
我知道我應該使用while循環,但不知道應該在哪裏開始循環。
帶循環。如果你搜索網站,你會發現一些例子http://stackoverflow.com/questions/34474882/read-input-until-a-certain-number-is-typed例如 – 2017-04-04 17:44:10