你不应该关门
Scanner
对于
System.in
因为它也关闭了
程序输入
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String confirmation, enteredName;
do {
System.out.print("What's your name again?");
enteredName = sc.nextLine();
System.out
.print("So, your name is " + enteredName + "?\nEnter 'yes' to confirm or 'no' to type a new name.");
confirmation = sc.nextLine();
if (confirmation.equalsIgnoreCase("yes") || confirmation.equalsIgnoreCase("yes.")) {
System.out.println("Confirmed, " + enteredName + ".\n\nNow launching.");
}
} while (confirmation.equalsIgnoreCase("no") || confirmation.equalsIgnoreCase("no."));
}
}
示例运行:
What's your name again?Arvind
So, your name is Arvind?
Enter 'yes' to confirm or 'no' to type a new name.no
What's your name again?Kumar
So, your name is Kumar?
Enter 'yes' to confirm or 'no' to type a new name.no.
What's your name again?Avinash
So, your name is Avinash?
Enter 'yes' to confirm or 'no' to type a new name.yes
Confirmed, Avinash.
Now launching.