Java 调用nextLine()的扫描程序不会发出NoSuchLineException
我正在尝试制作一个游戏,询问用户的名字,对这个名字表示怀疑,然后问用户三个问题,这是我的代码,上面有三个问题中的两个Java 调用nextLine()的扫描程序不会发出NoSuchLineException,java,Java,我正在尝试制作一个游戏,询问用户的名字,对这个名字表示怀疑,然后问用户三个问题,这是我的代码,上面有三个问题中的两个 private static final Scanner console = new Scanner(System.in); public static void main(String[] args) { System.out.println("Hello user! what is your name? "); String Name
private static final Scanner console = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Hello user! what is your name? ");
String Name = console.nextLine();
System.out.println("Really? " + Name + " is too weird to be a real name.");
confirmation();
Mascot();
Fence();
System.out.println("Thank you for playing the demo");
console.close();
}
public static void confirmation() {
System.out.print("is that REALLY your name? (type Y/N) ");
String yN = console.nextLine();
String a = yN;
Scanner scanner = new Scanner(a);
if (scanner.hasNext("Y")) {
System.out.println("I still dont belive you, so you will have to answer 3 riddles before you can continue to the game");
} else{
calledIt();
}
scanner.close();
}
public static void calledIt() {
System.out.println("I knew it!");
System.out.print("whats your real name? ");
String realName =console.nextLine();
System.out.println("" + realName + "sounds like a real name, but you lied the first time so you will need to answer riddles 3 to continue to the game");
}
public static void Mascot() {
System.out.println("what Is our school mascot?");
String b = console.nextLine();
if (b.toLowerCase().contains("tiger")){
System.out.println("Good, next riddle.");
} else{
System.out.println("You have failed");
}
}
public static void Fence() {
System.out.println("What runs around the whole yard without moving?");
String c = console.nextLine();
if (c.toLowerCase().contains("fence")){
System.out.println("Good, next riddle.");
} else{
System.out.println("You have failed");
}
}
}
这是当前输出(具有随机名称)
我希望它在确认()中接受Y或Y作为“是输入”。
如果有任何问题回答不正确,程序需要停止,而不是继续下一个问题。因为我与bob回答了第一个问题,所以应该说您失败了,然后停止。因为您在
确认()中调用scanner.close()
终止了系统。
流
method,您无法再读取流,因此调用String realName=console.nextLine();
将发出异常
解决方案:删除代码中的
scanner.close()
调用。与其创建多个扫描仪实例,不如准备一个从System.in读取,并在所有工作完成后将其关闭
private static final Scanner console = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Hello user! what is your name? ");
String Name = console.nextLine();
System.out.println("Really? " + Name + " is too weird to be a real name.");
confirmation();
Mascot();
System.out.println("Thank you for playing the demo");
console.close();
}
public static void confirmation() {
System.out.print("is that REALLY your name? (type Y/N) ");
String yN = console.nextLine();
String a = yN;
Scanner scanner = new Scanner(a);
if (scanner.hasNext("Y")) {
System.out.println("I still dont belive you, so you will have to answer 3 riddles before you can continue to the game");
} else{
calledIt();
}
scanner.close();
}
public static void calledIt() {
System.out.println("I knew it!");
System.out.print("whats your real name? ");
String realName =console.nextLine();
System.out.println("" + realName + "sounds like a real name, but you lied the first time so you will need to answer riddles 3 to continue to the game");
}
public static void Mascot() {
System.out.println("what Is our school mascot?");
String b = console.nextLine();
if (b.toLowerCase().contains("tiger")){
System.out.println("Good, next riddle.");
} else{
System.out.println("You have failed");
}
}
@MentalOverload,好的。您不需要关闭扫描仪的系统。在stream中,当您的程序关闭时,Java虚拟机将为您处理它。如何使它在声明您失败后停止程序
private static final Scanner console = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Hello user! what is your name? ");
String Name = console.nextLine();
System.out.println("Really? " + Name + " is too weird to be a real name.");
confirmation();
Mascot();
System.out.println("Thank you for playing the demo");
console.close();
}
public static void confirmation() {
System.out.print("is that REALLY your name? (type Y/N) ");
String yN = console.nextLine();
String a = yN;
Scanner scanner = new Scanner(a);
if (scanner.hasNext("Y")) {
System.out.println("I still dont belive you, so you will have to answer 3 riddles before you can continue to the game");
} else{
calledIt();
}
scanner.close();
}
public static void calledIt() {
System.out.println("I knew it!");
System.out.print("whats your real name? ");
String realName =console.nextLine();
System.out.println("" + realName + "sounds like a real name, but you lied the first time so you will need to answer riddles 3 to continue to the game");
}
public static void Mascot() {
System.out.println("what Is our school mascot?");
String b = console.nextLine();
if (b.toLowerCase().contains("tiger")){
System.out.println("Good, next riddle.");
} else{
System.out.println("You have failed");
}
}