【发布时间】:2014-11-07 14:30:21
【问题描述】:
我是 java 编程新手并使用 NetBeans IDE 8.0,我正在尝试编写一个循环遍历一个充满数字的 txt 文件 (numbers.txt) 的代码,找到所有低于 10 的数字,以及所有数字超过 30,然后计算这些数字的平均值,并输出类似“范围内数字的平均值是 __”
这是我目前得到的代码,因为我不太确定哪里出错了:
public class RangeAverage {
int average=0,num;
Scanner scan=new Scanner(new File("numbers.txt"));
while(scan.hasNextInt())
{
if((num=scan.nextInt()<10 || num >30)
{
average+=num;
}
}
System.out.println(" Average of numbers in range is "+average);
}
我得到的错误是:
Scanner scan=new Scanner(new File("numbers.txt")); (Lightbulb symbol)
-cannot find symbol
symbol: class Scanner
location: class RangeAverage
-cannot find symbol
symbol: class Scanner
location: class RangeAverage
-cannot find symbol
symbol: class Scanner
location: class RangeAverage
-----------
就行了
System.out.println(" Average of numbers in range is "+average); (a ! error)
-cannot find symbol
symbol: class out
location: class System
<identifier> expected
illegal start of type
-------------
任何帮助将不胜感激,谢谢!
【问题讨论】:
-
使用方法 ....将代码放入方法中
-
我真的是编程新手,所以不完全确定在那里做什么,抱歉哈哈
-
这不是平均数,只是一个总和
-
看来您已经掌握了 java 中的某些语法,所以我的意思是应有的尊重:您可能想浏览一下专注于面向对象编程的早期 java 教程:@987654321 @
-
@Pedram 不,这不是家庭作业..