为什么在使用do while循环时跳过了nextLine

0 java loops do-while

我必须为我的课程编写一个程序,该程序将用户输入作为家庭作业,实验室,测试等,将它们放在一个字符串中,并且有一个单独的类将它们分成单独的"双"数字并包含一个writeToFile方法来编写学生的成绩为档案.

我一直遇到问题.当我第一次询问用户他们的名字时,它工作正常,但如果用户决定再次进入do-while循环,则会跳过"你的名字是什么",它会消失直接到id.我知道它与最后一个不是字符串的输入有关,而解决方案就是放一个"keyboard.nextLine();" 在问题之上,但我尝试过,它甚至在询问问题之前只询问用户他们的名字..

import java.util.Scanner; 
    import java.io.*;
    import java.text.DecimalFormat;

    public class GradeApplication
    {
        public static void main(String[] args) throws IOException 
        {
            Scanner keyboard = new Scanner(System.in);

            //Define variables
            String name;
            int id;
            String homework;
            String labs;
            String tests;
            double project;
            double discussion;
          int answer;

            do
            {
                System.out.print("\nWhat is your name? ");
                name=keyboard.nextLine();

                System.out.print("\nWhat is your student ID? ");
                id=keyboard.nextInt();

                homework = keyboard.nextLine();
                System.out.println("\nPlease enter homework grades separated by spaces:");
                homework = keyboard.nextLine();

                System.out.println("Please enter lab grades separated by spaces:");
                labs = keyboard.nextLine();

                System.out.println("Please enter test grades separated by spaces:");
                tests = keyboard.nextLine();

                System.out.println("Please enter project grade:");
                project = keyboard.nextDouble();

                System.out.println("Please enter discussion grade:");
                discussion = keyboard.nextDouble();

                System.out.println("\nResults: ");
                //Call toString method
                Student_Khazma s = new Student_Khazma(name,id,homework,labs,tests,project,discussion);
                System.out.print(s.toString()); 

     //Open file         
         PrintWriter outputFile= new PrintWriter("gradeReport.txt");
         System.out.println(s.writeToFile());
         outputFile.close();

         System.out.print("\n\nWould you like to see your grade report again? (1 is yes, 2 is no): ");
         answer=keyboard.nextInt();

             System.out.print("\n\nWould you like to see your grade report again? (1 is yes, 2 is no): ");
             answer=keyboard.nextInt();

                }while(answer==1);

        }
    }
Run Code Online (Sandbox Code Playgroud)

acd*_*ior 6

你的最后一次电话Scanner#nextInt():

    ...
    System.out.print("\n\nWould you like to see your grade report again? (1 is yes, 2 is no): ");
    answer=keyboard.nextInt();

} while(answer==1);
Run Code Online (Sandbox Code Playgroud)

不消耗换行符,因此它会被传递给你的第一次调用Scanner#nextLine().因此,扫描操作不会阻止等待输入(有关详细信息,请参阅javadoc).要解决它,您需要添加keyboard.nextLine();:

    ...
    System.out.print("\n\nWould you like to see your grade report again? (1 is yes, 2 is no): ");
    answer=keyboard.nextInt();
    keyboard.nextLine(); // <== line added here
} while(answer==1);
Run Code Online (Sandbox Code Playgroud)

这样你的第一次调用nextLine()就会阻塞输入(当循环重启时).