Java:我不明白为什么这没用

时间:2018-08-10 06:54:32

标签: java

是Java的新手。任务是创建一个StudentGrades应用程序,该应用程序提示用户本学年完成的课程数量,然后提示用户输入各门课程获得的成绩。然后,StudentGrades应用程序应在一行上显示符合高成就奖要求的成绩(> 93),在下一行显示需要提高的成绩(<70)。

输出显示如下:

/StudentGrade.java:46: error: cannot find symbol
        if(scores[i]<70) {
                  ^
  symbol:   variable i
  location: class StudentGrade
/StudentGrade.java:47: error: cannot find symbol
        System.out.print(scores[i]+ " ");
                                ^
  symbol:   variable i
  location: class StudentGrade
2 errors

我该怎么办?我很困惑

这是我的代码:

import java.util.Scanner;

public class StudentGrade {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        // Prompt the user to enter the total number of courses
        System.out.print("Enter the number of courses completed this school year: ");
        int[] scores = new int[input.nextInt()];

        // Prompt the user to enter all the scores
        System.out.print("Enter " + scores.length + " score(s): ");
        for (int i = 0; i < scores.length; i++) {
            scores[i] = input.nextInt();
        }

        System.out.println("Grades that qualify for High Achievement Award (above 93%): ");
    for(int i=0; i< scores.length; i++) {
        if(scores[i]>93) {
            System.out.print(scores[i]+ " ");
        }}

    System.out.println("");
    System.out.println("Grades that need improvement (below 70%): ");
    for(int l=0; l<scores.length;l++) {
        if(scores[i]<70) {
        System.out.print(scores[i]+ " ");
        }

    }
    }

}

1 个答案:

答案 0 :(得分:2)

在此循环中:

for(int l=0; l<scores.length;l++) {
    if(scores[i]<70) {
    System.out.print(scores[i]+ " ");
    }

您不使用i作为变量名,而是将其切换为l

您的变量仅存在于它们各自的范围内,这意味着一旦for循环开始,就不再有i

将代码更改为:

for(int l=0; l<scores.length;l++) {
    if(scores[l]<70) {
    System.out.print(scores[l]+ " ");
    }

然后重试。