由於某種原因,我不知道,當我的代碼循環回到開始一些字符串輸入選項丟失..這是我的問題。當代碼從一個循環重複時,用戶輸入選項丟失
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();
}
}
}
我怎麼能允許輸入學生姓名,一旦循環? 謝謝