Java 基本';对于循环';编译错误

Java 基本';对于循环';编译错误,java,loops,for-loop,Java,Loops,For Loop,我试着把“I”改成“j”。。。。但这也不起作用。有什么想法吗?谢谢。System.out.println(名称[j]+“售出”+销售[j]);aa尝试从代码中删除aa。System.out.println(名称[j]+“已售出”+sales[j]);aa 我认为,aa需要删除这行代码末尾有两个外来字符需要删除 Sales.java:36: error: cannot find symbol if (sales[i] >= avg){ // Indicates wheth

我试着把“I”改成“j”。。。。但这也不起作用。有什么想法吗?谢谢。

System.out.println(名称[j]+“售出”+销售[j]);aa
尝试从代码中删除aa。

System.out.println(名称[j]+“已售出”+sales[j]);aa


我认为,
aa
需要删除

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

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
,因为在特定行的作用域中没有具有该名称的变量。先前声明的
i
仅在第一个for循环期间存在

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

System.out.println (name[j] + " sold " + sales[j]);aa
for(int i=0;i=avg){//该人的销售额是否高于平均水平。
System.out.println(名称[i]+“销售额高于平均销售额”);
}
}

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

基本上,您使用的是一个for循环,它在代码的开头声明了一个int
i
。然而,当这个for循环完成时,int
i
现在就消失了

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.");
    }
}
这将为您提供变量
i
,该变量在范围内,可以按您尝试的方式使用

另外,作为补充说明,您可能希望将for循环更改为使用
sales.size
而不是显式数字。如果以后更改阵列的大小,这将使您更容易操作

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.");
    }
}
for(inti=0;i
当然,它找不到它,因为您在for循环之外引用变量“i”。for(int i=0;…)定义的变量仅对for循环可见,不在其外部。

System.out.println(name[j]+“saled”+sales[j]);aa
您应该正确设置此代码的格式,这有点难读。另外,我认为您结束了包含
I
的for循环,因此在
sales[I]行,您的
j
变量也是如此。我知道你说你试过了,但是它和
I
有同样的问题,在你使用它的时候它超出了范围。哦,我明白你的意思了。我试着玩玩它。ThxI为您提供了以下可能的解决方案。这将有助于你了解我所描述的。我意识到在发布这篇文章之前。。。在删除它之后,得到了一个不同的符号错误。(我帖子里的那个)。很抱歉给你带来困惑。。。关于这个错误有什么想法吗?谢谢
for (int i=0; i<5; i++) {
    System.out.println ("What is the name of the person?");
} // <-- After this your int i is gone
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.");
    }
}
 for (int i = 0; i < sales.length; i++) {  }