有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java输入后,println不会输出任何内容

我正在做一个游戏,它会提示你输入你的名字,在你输入名字后,它会要求你输入“N”或“Y”来确认。按N键后,它不会打印任何内容,尽管它会接收输入,但不会提示您这样做,因为它不会打印任何其他内容。只有Y能工作。我什么都试过了,但都没用

这是我用来确认名字的代码:

private static void comfirmName() {
    System.out.println("Is " + name +  " your name?");

    try {
        Thread.sleep(1000);
    } catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
    }

    System.out.println("Y/N");

    if (input.nextLine().toUpperCase().equals("Y")) {
        System.out.println("There is something you should know...");    
    }

    if (input.nextLine().toUpperCase().equals("N")) {
        System.out.println("Enter your name:");
        name = input.nextLine();
        System.out.println("Is " + name + " your name?");
    }

    if (!input.nextLine().toUpperCase().equals("N") && !input.nextLine().toUpperCase().equals("Y")) {
        System.out.println("Please enter Y or N");
    }
}

这是输出:

Welcome to Enchanted Mage!
Here we will venture into the dangers of this planet!
First of all, you must tell me your name, venturer!
Type in your name:
ots wng
Hello there ots wng!
Is ots wng your name?
Y/N
n
otswng
nothing is hapening
ummm
Please enter Y or N

BUILD SUCCESSFUL

虽然没有错误,但输出任何结果都很烦人


共 (1) 个答案

  1. # 1 楼答案

    确认书只需阅读一次

    private static void comfirmName() {
        System.out.println("Is " + name +  " your name?");
    
        System.out.println("Y/N");
    
        String confirmation = input.nextLine().toUpperCase(); //Read only once the user confirmation
    
        if (confirmation.equals("Y")) {
            System.out.println("There is something you should know...");
        }
    
        if (confirmation.equals("N")) {
            System.out.println("Enter your name:");
            name = input.nextLine();
            System.out.println("Is " + name + " your name?");
        }
    
        if (!confirmation.equals("N") && !confirmation.equals("Y")) {
            System.out.println("Please enter Y or N");
        }
    }
    

    输出

    Is Dan your name?
    Y/N
    n
    Enter your name:
    Dan
    Is Dan your name?