我一直在一個問題上停留了一段時間,程序沒有做我認爲應該做的事情。Java掃描器跳過迭代
當我運行程序併到達要求您輸入課程名稱的部分時,程序將跳過第一次迭代,或者取決於輸入了多少課程。它只允許在最後一次迭代中輸入。對於以下for循環,程序跳過它們而不允許輸入。
我的問題是,是for循環不正確還是字符串數組不正確地輸入信息到他們的下標?
import java.util.Scanner; //Needed for Scanner class
public class StudentRecords
{
public static void main(String[] args)
{
int courses;
int students;
int[] course = new int[5];
int[] student = new int[5];
double GPA = 0;
String[] courseNumber = new String[5];
double[] creditHours = new double[5];
String[] letterGrade = new String[5];
//Scanner object for user input
Scanner kb = new Scanner(System.in);
System.out.println("This program will help you determine the GPA \n"
+ "for each student entered.");
System.out.println("");
System.out.println("How many student's GPA are you going to calculate?");
System.out.print("Enter amount of students (Maximum of 5 students): ");
students = kb.nextInt();
student = new int[students];
System.out.println("");
for(int index = 0 ; index < student.length; index++)
{
System.out.print("Student " + (index + 1) + " information: ");
System.out.println("");
System.out.print("How many courses did student " +
(index + 1) + " take? ");
courses = kb.nextInt();
course = new int[courses];
for(int i = 0; i < course.length; i++)
{
System.out.println("What is the name of course #" + (i + 1));
courseNumber[i] = kb.nextLine();
}
for(int i = 0; i < course.length; i++)
{
System.out.println("How many credit hours is " + courseNumber[i]);
creditHours[i] = kb.nextDouble();
}
for(int i = 0; i < course.length; i++)
{
System.out.println("What is the final letter grade for " + courseNumber[i]);
letterGrade[i] = kb.nextLine();
}
for(i = 0; i < student.lenght<
}
}
}
P.S.這是我的工作的問題:
寫具有以下輸入,所有這些都存儲在 陣列(大小爲5)的程序。首先,該學期學生參加了多少門課程 (不能大於5)。在每個學生的ARRAYS中存儲 課程編號/名稱(例如ICT 435),學分(1-4)和 字母等級(A-F)。確定學期的GPA。
在使用next(),nextInt()或其他nextFoo()方法之後跳過nextLine()(http:// stackoverflow。com/questions/13102045/skip-nextline-after-using-next-nextint-or-other-nextfoo-methods) – Arjan
@Edù你可以接受答案,如果它解決了你的問題。 – Kaushal28