首页 > 解决方案 > 调用 nextLine() 的扫描仪发出 NoSuchLineException

问题描述

我正在尝试制作一个询问用户姓名的游戏,对姓名持怀疑态度,然后向用户询问三个问题,这是我到目前为止的代码,上面有三分之二的问题。

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");
    }
    
}

}

这是当前输出(带有随机名称)

Hello user! what is your name? 
bob
Really? bob is too weird to be a real name.
is that REALLY your name? (type Y/N) Y
I still dont belive you, so you will have to answer 3 riddles before you can continue to the game
what Is our school mascot?
bob
You have failed
What runs around the whole yard without moving?

我希望它接受 Y 或 y 作为确认()中的“是”输入。如果任何问题的回答不正确,程序需要停止而不是继续下一个问题。因为我用 bob 回答了第一个问题,它应该说你失败了然后停止。

标签: java

解决方案


与其创建许多 Scanner 实例,不如准备一个从 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");
    }
    
}

推荐阅读