当我尝试运行此代码时noOfSub()
方法正确执行;
但GC()
方法面临以下问题:
Enter the number of subjects:
2
Enter Your Subject 1 Grade:
s
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at GPA.GC(GPA.java:21)
at GPA.main(GPA.java:35)
Java Result: 1
这是我的代码:
import java.util.Scanner;
public class GPA {
public int noOfSubjects;
public int i=1;
Scanner gradeInput = new Scanner(System.in);
String[] grade = new String[noOfSubjects];
int[] credit = new int[noOfSubjects];
public void noOfSub() {
System.out.println("Enter the number of subjects:");
Scanner sub = new Scanner(System.in);
noOfSubjects = sub.nextInt();
}
public void GC() {
while(i<=noOfSubjects)
{
System.out.println("Enter Your Subject "+i+" Grade:" );
grade[i] = gradeInput.nextLine();
System.out.println("Enter the Subject "+i+" Credit:");
credit[i] = gradeInput.nextInt();
i++;
}
}
public static void main(String[] args) {
GPA obj = new GPA();
obj.noOfSub();
obj.GC();
}
}
答案 0 :(得分:1)
当你这样做时:
public int noOfSubjects;
noOfSubjects
设置为0
,即default value
所以当你有以下代码时:
String[] grade = new String[noOfSubjects];
它基本上意味着,
String[] grade = new String[0]; //create a new String array with size 0
为你创建一个空数组。
所以当你这样做时,
grade[i] = gradeInput.nextLine(); //where i is 1
你得到:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at GPA.GC(GPA.java:21)
at GPA.main(GPA.java:35
因为1
中没有索引String[] grade
。
答案 1 :(得分:0)
如果要从用户获取数组的大小,请在从stdin获取数组后创建该数组。否则,它将创建一个大小为0的数组,这是java中int的默认值。
答案 2 :(得分:0)
分离您的声明和初始化
String[] grade = null;
int[] credit = null;
...
noOfSubjects = scan.nextInt();
grade = new String[noOfSubjects];
credit = new int[noOfSubjects];
答案 3 :(得分:0)
数组初始化时出现问题。您可以在从用户那里获取输入后初始化数组。
例如:
public void noOfSub() {
System.out.println("Enter the number of subjects:");
Scanner sub = new Scanner(System.in);
noOfSubjects = sub.nextInt();
grade = new String[noOfSubjects];
credit = new int[noOfSubjects];
}
改变你的状态。而不是你使用
while(i < noOfSubjects)
并设置i = 0
答案 4 :(得分:0)
为什么不使用ArrayList,因为不知道数组的大小
public class GPA {
public int noOfSubjects;
public int i=0;
Scanner gradeInput = new Scanner(System.in);
List<String> grade = new ArrayList<>();
List<Integer> credit = new ArrayList<>();
public void noOfSub(){
System.out.println("Enter the number of subjects:");
Scanner sub = new Scanner(System.in);
noOfSubjects = sub.nextInt();
}
public void GC(){
while(i<noOfSubjects)
{
System.out.println("Enter Your Subject "+(i+1)+" Grade:" );
grade.add(gradeInput.nextLine());
System.out.println("Enter the Subject "+(i+1)+" Credit:");
credit.add(gradeInput.nextInt());
gradeInput.nextLine();
i++;
}
}
public static void main(String[] args) {
GPA obj = new GPA();
obj.noOfSub();
obj.GC();
}
}
注意:我在gradeInput.nextLine()
之后添加了i++
,因为Scanner.nextInt()
方法不会消耗您输入的最后一个换行符,因此会消耗换行符在下一次调用Scanner.nextLine()
时,我会在gradeInput.nextLine()
之后触发一个空白的gradeInput.nextInt()
来调用该行的剩余部分,包括换行符
答案 5 :(得分:0)
由于noOfSubjects具有运行时值,因此代码应为:
import java.util.Scanner;
公共类GPA {
public int noOfSubjects;
public int i = 0;
Scanner gradeInput = new Scanner(System.in);
String[] grade;
int[] credit;
public void noOfSub() {
System.out.println("Enter the number of subjects:");
Scanner sub = new Scanner(System.in);
noOfSubjects = sub.nextInt();
grade = new String[noOfSubjects];
credit = new int[noOfSubjects];
}
public void GC() {
while (i < noOfSubjects) {
System.out.println("Enter Your Subject " + (i + 1) + " Grade:");
grade[i] = gradeInput.next();
System.out.println("Enter the Subject " + (i + 1) + " Credit:");
credit[i] = gradeInput.nextInt();
i++;
}
for (int j = 0; j < grade.length; j++) {
System.out.println(grade[j] + " " + credit[j]);
}
}
public static void main(String[] args) {
GPA obj = new GPA();
obj.noOfSub();
obj.GC();
}
}