0

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

虽然我一直遇到问题..当我第一次询问用户他们的名字时,它工作正常,但如果用户决定再次进入 do-while 循环,“你的名字是什么”被跳过并继续直接去身份证。我知道这与最后一个输入不是字符串有关,解决方案是放置一个“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);

        }
    }
4

1 回答 1

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()将阻止输入(当循环重新启动时)。

于 2013-07-20T04:35:07.300 回答