基本的"for 循环"编译错误


import java.util.Scanner; // Scanner tool is imported.
public class Sales   
{
    public static void main (String[] args) // Main.
    {
        Scanner scan = new Scanner(System.in);   // Scanner object is instantiated.
        String[] name = new String[5]; // First array for names is created.
        double[] sales = new double[5]; // Second array for sales is created.
        for (int i=0; i<5; i++) {    // For loop to ask each question 5 times.
            System.out.println ("What is the name of the person?");
            name[i] = scan.nextLine();
            System.out.println ("How much did he/she sell?");
             sales[i] = scan.nextDouble();
        }
        for (int j=0; j<5; j++) {    // loop to print the salesperson and amount of sales.
           System.out.println (name[j] + " sold " + sales[j]);aa
        }

        int sum = 0;   // The sum is calculated using a for loop.
        for (double k : sales){
            sum+=k;
        }
        double avg = sum / 5;  // The average is calculated.
        System.out.println ("The average sales is: " + avg);
        if (sales[i] >= avg){  // whether or not the person sold greater than average.
            System.out.println (name[i] + " sold more than the average sales.");
        }
    }
}

编译错误:

Sales.java:36: error: cannot find symbol
         if (sales[i] >= avg){  // Indicates whether or not the person sold greater than average.
                   ^   symbol:   variable i   location: class Sales Sales.java:37: error: cannot find symbol
         System.out.println (name[i] + " sold more than the average sales.");
                                  ^   symbol:   variable i   location: class Sales 2 errors

我尝试将"i"更改为"j"....但这也没有用。有什么想法吗?谢谢。

基本上发生的事情

是你正在使用一个 for 循环,它在代码的开头声明一个 int i。但是,当这个 for 循环完成时,int i现在消失了。

for (int i=0; i<5; i++) {
    System.out.println ("What is the name of the person?");
} // <-- After this your int i is gone

该 int,因为它是在 for 循环中声明的,所以仅在该循环的生存期内持续存在。稍后在代码中,您将尝试使用不再存在的相同 int i。这与将其更改为j不起作用的确切原因相同。当您到达问题区域时,您在其中声明 int j 的循环也已完成。

尝试在问题区域添加另一个 for 循环,如下所示:

for (int i = 0; i < 5; i++) {
    if (sales[i] >= avg){  // whether or not the person sold greater than average.
        System.out.println (name[i] + " sold more than the average sales.");
    }
}

这将为您提供范围内的变量i,可以按照您尝试的方式使用。

此外,作为旁注,您可能希望将 for 循环更改为使用 sales.size 而不是显式数字。如果您稍后更改数组的大小,这将使您更容易。

 for (int i = 0; i < sales.length; i++) {  }

此行代码末尾有两个需要删除的外来字符。

System.out.println (name[j] + " sold " + sales[j]);aa

它还抱怨i因为该特定行的范围内没有具有该名称的变量。您之前声明的i仅在第一个 for 循环的持续时间内存在。

一个简单的解决方法是将该 if 语句包装在另一个循环中。无论如何,您可能都需要一个,因为您试图找出谁的销量高于平均水平:

for (int i = 0; i < sales.length; i++){
    if (sales[i] >= avg){  // whether or not the person sold greater than average.
        System.out.println (name[i] + " sold more than the average sales.");
    }
}

编辑:这与您的问题无关,但您在循环的后续运行中将跳过对该人姓名的提示。您可能希望在调用 nextDouble() 之后添加另一个scan.nextLine()以使用尾随换行符。

当然,

它找不到它,因为您在 for 循环之外引用了变量"i"。由 for (int i=0;...) 定义的变量仅对 for 循环可见,在 for 循环外不可见。

System.out.println (name[j] + " sold " + sales[j]);aa尝试从代码中删除 AA。

System.out.println (name[j] + " sold " + sales[j]);aa

我认为aa需要删除

最新更新