调试程序以分级将字符串拆分为数组的学生

时间:2019-03-19 00:15:05

标签: java arrays

我在调试以下代码时遇到麻烦,因此输入的是4个人和4个单独的等级,中间有空格,这些空格会根据最高分数返回学生的等级,但是我不断遇到错误。有人请帮忙。谢谢!

输出:

Enter the number of students: 4 
Enter 4 scores: 55 48 70 58 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 
     Index 4 out of bounds for length 4 at Ex7_1.main(Ex7_1.java:15) 
import java.util.*;
public class Ex7_1 {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the number of students: ");

    int num = Integer.parseInt(in.nextLine()) + 1;
    System.out.printf("Enter %d scores: ", 4);

    String input = in.nextLine();
    String[] sArray = input.split(" ");
    int[] array = new int[num];

    for (int i = 0; i < num; i++) {
      String sNum = sArray[i];
      array[i] = Integer.parseInt(sNum);
    }

    int highest = 0;

    for (int i = 0; i <= num; i++) {
      if (array[i] >= highest) {
        highest = array[i];
      }
    }

    for (int i = 0; i <= num; i++) {
      int score = array[i];
      if (score >= (highest - 10)) {
        System.out.printf("Student %d score is %d and grade is A", i, score);
      } else if (score >= (highest - 20)) {
        System.out.printf("Student %d score is %d and grade is B", i, score);
      } else if (score >= (highest - 30)) {
        System.out.printf("Student %d score is %d and grade is C", i, score);
      } else if (score >= (highest - 40)) {
        System.out.printf("Student %d score is %d and grade is D", i, score);
      } else {
        System.out.printf("Student %d score is %d and grade is F", i, score);
      }
    }
  }
}

在两种情况下,将<=设为<后,此代码都会显示错误。我不知道如何解决该错误。请帮忙。谢谢!

3 个答案:

答案 0 :(得分:0)

看来您的问题不在查看数组的任何部分中,而是在设置数组内容的地方。

for (int i = 0; i < num; i++) {
  String sNum = sArray[i];
  array[i] = Integer.parseInt(sNum);
}

在本节中,您似乎尝试访问sArray的索引4处的项,该项不存在,因为数组的长度为4,因此最大索引为3。 >

此外,您还要在开头附近的num处加1,如果在for循环中使用<而不是<=,则没有必要,并且会导致您使用索引1到4,而不是您可能想要的0到3。

int num = Integer.parseInt(in.nextLine()) + 1; // You don't need this +1

最后,无论您实际需要多少分,您要求打分的打印陈述将始终打印为4。您可能希望将num传递到该打印语句中,因此它实际上会打印出您期望的分数。

System.out.printf("Enter %d scores: ", 4); // Replace this 4 with num

希望这会有所帮助!

答案 1 :(得分:0)

您在每个<=中使用了for loop运算符。首先,几乎每个人都对这部分感到困惑数组从0索引开始。因此,您不必使用>=,而在>中使用for loops

第二,如果您总是输入4个学生的分数,那么不要问这个:

System.out.println("Enter the number of students: ");

如果用户输入“ 5”怎么办。你会像这样:

Enter the number of students: 5
Enter 4 scores:

所以,这没有什么意义。最后,在代码的打印段中放置一个\n,它将使输出更加清晰和易于阅读:

System.out.printf("\nStudent %d score is %d and grade is A", i, score); // "\n" added

答案 2 :(得分:0)

避免ArrayIndexOutOfBoundException:-

int num = Integer.parseInt(in.nextLine())+1; //从该行中删除+ 1
在for循环中使用<<代替<=

为避免NumberFormatException:-

修剪字符串,并在拆分时使用与任意数量的空格字符匹配的正则表达式。
\\ s +-匹配一个或多个空格字符的序列。

尝试以下代码:

        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number of students: ");

        int num = Integer.parseInt(in.nextLine().trim());
        System.out.printf("Enter %d scores: ", num);

        String input = in.nextLine().trim();
        String[] sArray = input.split("\\s+");
        int[] array = new int[num];

        for (int i = 0; i < num; i++) {
            String sNum = sArray[i];
            array[i] = Integer.parseInt(sNum);
        }

        int highest = 0;

        for (int i = 0; i < num; i++) {
            if (array[i] >= highest) {
                highest = array[i];
            }
        }

        for (int i = 0; i < num; i++) {
            int score = array[i];
            if (score >= (highest - 10)) {
                System.out.printf("Student %d score is %d and grade is A\n", i+1, score);
            } else if (score >= (highest - 20)) {
                System.out.printf("Student %d score is %d and grade is B\n", i+1, score);
            } else if (score >= (highest - 30)) {
                System.out.printf("Student %d score is %d and grade is C\n", i+1, score);
            } else if (score >= (highest - 40)) {
                System.out.printf("Student %d score is %d and grade is D\n", i+1, score);
            } else {
                System.out.printf("Student %d score is %d and grade is F\n", i+1, score);
            }
        }
    }