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

时间:2013-07-20 04:29:17

标签: 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);

        }
    }

1 个答案:

答案 0 :(得分: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);

不使用换行符,因此会将其传递给您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);

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