当代码从循环重复时,用户输入选项将丢失

时间:2013-12-03 18:47:11

标签: java

由于某些我不知道的原因,当我的代码循环回到开始时,一些字符串输入选项丢失了..这是我的问题。

Enter student's name: 
Bob
Enter student's subject: 
Maths
Enter student's number: 
12345
Enter level student last completed (0-3): 
0
Would you like to enter data for another student? Yes/No 
Yes
Enter student's name: 
Enter student's subject: 
English        
Enter student's number: 
55677
Enter level student last completed (0-3): 
0

正如您所看到的那样,当循环再次循环以允许用户再次输入详细信息时它会打印出来

Enter student's name: 
Enter student's subject: 
English        
Enter student's number: 
55677
Enter level student last completed (0-3): 
0

同时,只允许输入主题而不是名称,跳过学生姓名的输入选项。之后的字段正常工作。

这是我的代码

    import java.util.*;

public class StudentData 
{
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        ArrayList<Student> studentList = new ArrayList<Student>();
        String yesNo; // = "true";
        int level = 0;

        do
        {
            int averageResult = 0;
            int result1 = 0;
            int result2 = 0;
            int result3 = 0;

//          in.next();
            System.out.println("Enter student's name: ");
            String name = in.nextLine();
            System.out.println("Enter student's subject: ");
            String subject = in.nextLine();
            System.out.println("Enter student's number: "); 
            int number = in.nextInt();

            do
            {
                System.out.println("Enter level student last completed (0-3): "); 
                 level = in.nextInt();
                }
            while (level > 3 || level < 0);
            if (level > 0)
            {
                if (level == 3)
                {
                System.out.println("Enter result for level 3: "); 
                result3 = in.nextInt();
                System.out.println("Enter result for level 2: "); 
                result2 = in.nextInt();             
                System.out.println("Enter result for level 1: "); 
                result1 = in.nextInt();
                }
            else if (level == 2)
            {
                System.out.println("Enter result for level 2: "); 
                result2 = in.nextInt();
                System.out.println("Enter result for level 1: "); 
                result1 = in.nextInt(); 
                }   
            else if (level == 1)    
            {
                System.out.println("Enter result for level 1: "); 
                result1 = in.nextInt();
                }

//          averageResult = 0;
            averageResult= ((result1 + result2 + result3) / level); 
            }
//          System.out.println(averageResult); 
            else
            {

            }
            Student s = new Student(name,subject,number,level,result1,result2,result3,averageResult);   
            studentList.add(s);



            do
            {   
                System.out.println("Would you like to enter data for another student? Yes/No ");
                yesNo = in.next();
                }
            while (!yesNo.equalsIgnoreCase("YES") && !yesNo.equalsIgnoreCase("NO"));
            }   
        while (yesNo.equalsIgnoreCase("YES"));




        for(int i = 0; i < studentList.size(); i++)
        {
            System.out.println("Student's name: "+studentList.get(i).getName());
            System.out.println("Student's subject: "+ studentList.get(i).getSubject());
            System.out.println("Student number: "+studentList.get(i).getNumber());
            System.out.println("Level which student has completed: "+studentList.get(i).getLevel());
            System.out.println("Result for level 1:"+studentList.get(i).getResults1());
            System.out.println("Result for level 2:"+studentList.get(i).getResults2());
            System.out.println("Result for level 3:"+studentList.get(i).getResults3());
            System.out.println("Average result for all completed levels: "+studentList.get(i).getAverageResults());
            System.out.println();
            }
        }
    }

一旦循环,我如何允许输入学生姓名? 感谢

3 个答案:

答案 0 :(得分:0)

之前我遇到过同样的问题。我认为扫描仪的nextLine()方法存在问题。 解决此问题的一种简单方法是使用您遇到问题的nextLine()方法复制,或者您可以使用另一个对象从用户获取输入。

答案 1 :(得分:0)

而不是使用nextLine()获取名称和主题,您可以使用next()来解决您的问题

答案 2 :(得分:0)

执行以下更改,它将按预期工作

Scanner in; //do not create Scanner instance here
ArrayList<Student> studentList = new ArrayList<Student>();
String yesNo; // = "true";
int level = 0;

do
{
    in = new Scanner(System.in);//instead create here