Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么确认语句要打印到控制台两次?_Java - Fatal编程技术网

Java 为什么确认语句要打印到控制台两次?

Java 为什么确认语句要打印到控制台两次?,java,Java,每当我运行程序时,一切正常,但由于某种原因,确认打印发生了两次,我不知道为什么。任何帮助都将不胜感激 public static void main(String[] args) { Scanner input = new Scanner(System.in); String Z = "Z"; int number; String Znumber = ""; String Confirmation = "";

每当我运行程序时,一切正常,但由于某种原因,确认打印发生了两次,我不知道为什么。任何帮助都将不胜感激

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String Z = "Z";
        int number;
        String Znumber = "";
        String Confirmation = "";
        int ExamType;

        // Ask user for Z number
        do {
            System.out.println("Please enter your Z number. Example: 12345678");
            number = input.nextInt();
            Znumber = Z + number;

        } while ((Znumber.length() != 9));

        //Confirm Z number entry
        do {
            System.out.printf(
                    "Your Z number is: %s. Is this correct? Enter Y/N: \n",
                    Znumber);
            Confirmation = input.nextLine();
        } while (!Confirmation.equalsIgnoreCase("y"));

        // Ask user which exam they would like to take
        ExamType = 0;
        Confirmation = "";

        System.out.println("Which exam would you like to take? Select 1-3: ");
        System.out.println("1: English");
        System.out.println("2: Spanish");
        System.out.println("3: Math");
        ExamType = input.nextInt();

        do {
            System.out.printf("You selected %s. Are you sure? Enter Y/N: \n",
                    ExamType);
            Confirmation = input.nextLine();

        } while (!Confirmation.equalsIgnoreCase("y"));

        // Begin exam
        if (ExamType == 1) {

            System.out.println("Welcome to the English exam!");
            // Start code from JavaExam.java

        } else if (ExamType == 2) {

            System.out.println("Welcome to the Spanish exam!");
            // Start code from MathExam.java

        } else if (ExamType == 3) {

            System.out.println("Welcome to the Math exam!");
            // Start code from EnglishExam.java
您的第一个循环//向用户询问Z编号

不读取最后一个'\n'

我认为您需要在该循环中添加下一行。

而不是Confirmation=input.nextLine;,使用Confirmation=input.next;你应该表现得很好。测试和确认


在do while循环中确实不需要nextLine。

在do while中,当您第一次运行打印时,您的Z编号是:%s。这是正确的吗?输入Y/N:。在那之后,情况就变了!Confirmation.equalsIgnoreCasey在本例中被评估为true,因此请尝试在第二次运行循环do

     do {
       System.out.printf("Your Z number is: %s. Is this correct? Enter Y/N: \n",
               ....
        } while (!Confirmation.equalsIgnoreCase("y"));

你把哪条消息称为确认消息?你的Z号码是。。。或者您选择了%s。你确定吗?在使用nextInt之后,你需要调用nextLine来使用该输入行的其余部分。否则,nextLine将立即返回其缓冲输入,您的do循环将进入第二次迭代。回答正确。非常感谢您的时间。@WillCrayger没问题,伙计,欢迎来到SO。
     do {
       System.out.printf("Your Z number is: %s. Is this correct? Enter Y/N: \n",
               ....
        } while (!Confirmation.equalsIgnoreCase("y"));