遇到java循环问题

时间:2018-06-13 22:34:03

标签: java arrays loops printing

所以我试图按顺序打印出我的所有数组。假设用户进入类并读取它。然后它询问该类中的学分数,然后再次读取。然后它要求提供字母等级,但它也打印出其他不应该存在的东西,它会弄乱程序。

示例:

Enter the name of class: 
HIST101
Enter the credits associated with HIST101: 
3
Enter the grade earned for HIST101: 
Enter the name of class: // error here//
A
Enter the credits associated with A: 

因此,错误开始是因为它读取了类的第二个名称而不是字母等级。我是新手,所以请理解。代码将在

之下

int numStudents = 4;
		String [] className = new String [numStudents];
		int [] credsEarn = new int [numStudents];
		String [] gradeEarn = new String [numStudents];
		
		for(int i = 0; i < numStudents; i++) {
			System.out.println("Enter the name of class: ");
			className[i] = scnr.nextLine();
			System.out.println("Enter the credits associated with " + className[i] + ": ");
			credsEarn[i] = scnr.nextInt();
			System.out.println("Enter the grade earned for " + className[i] + ": ");
			gradeEarn[i] = scnr.nextLine();
		}

0 个答案:

没有答案