Я уже давно застрял в проблеме, и программа не делает то, что я считаю, что она должна делать.Java-сканер, пропуская итерации
Когда я запускаю программу и попадает в ту часть, где он запрашивает имя курса, программа пропускает первую итерацию или другую, зависящую от того, сколько курсов было введено. Он допускает ввод только на последней итерации. Для следующих циклов программа пропускает их, даже не допуская ввода.
Мои вопросы: - неправильные петли или массивы String, которые не вводят информацию правильно в их индекс?
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 для семестра.
Возможный дубликат [Пропуск nextLine() после использования next(), nextInt() или других методов nextFoo()] (http: // stackoverflow.com/questions/13102045/skipping-nextline-after-use-next-nextint-or-other-nextfoo-methods) – Arjan
@ Edù вы можете принять ответ, если он решил вашу проблему. – Kaushal28