【问题标题】:Looping over a txt file, finding numbers within a range and their average循环遍历 txt 文件,查找范围内的数字及其平均值
【发布时间】: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 不,这不是家庭作业..

标签: java loops range average


【解决方案1】:

您不能将这些代码放在类区域中。使用方法。您可以在外面声明变量,但过程应该在方法内部发生。并且类应该有一个主要方法来运行。并且您正在计算总和而不是平均您需要将总和除以循环时间才能得到平均值。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class RangeAverage{

    public static void main(String[] args) throws FileNotFoundException {
        int average = 0, num = 0, i=0;
        Scanner scan = new Scanner(new File("numbers.txt"));
        while (scan.hasNextInt()) {
            if (((num = scan.nextInt()) < 10 || num > 30)){
                average += num;
                i++;
            }
        }
        System.out.println(" Average of numbers in range is " + (average/i));

    }
}

根据您的评论错误说该目录中没有这样的命名文本文件。因此将文件添加到目录或提供文本文件的完整路径。您需要转义斜杠\ 例子 如果你的文件路径是

C:\Users\Madhawa.se\Desktop\numbers.txt 

那么路径应该是,

Scanner scan = new Scanner(new File("C:\\Users\\Madhawa.se\\Desktop\\numbers.txt"));

【讨论】:

  • @peter.petrov 哦,是的。但现在至少可以编译了。谢谢我会修复它
  • 感谢您的回复!使用它可以消除所有错误,但是当我构建和运行代码时,我得到了这个:运行:线程“main”java.io.FileNotFoundException中的异常:java.txt(系统找不到指定的文件)。 io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:131) at java.util.Scanner.(Scanner.java:611) at test2.RangeAverage.main( RangeAverage.java:17) Java 结果:1 构建成功(总时间:2 秒)
  • @CF0rd 你得到了什么?
  • @CF0rd 给出你的文本文件的完整路径。错误说在 derectry 中没有名为 numbers.txt 的文件。你的文本文件的路径是什么?例如 C:\Users\Madhawa .se\Desktop\number.txt
【解决方案2】:

将您的代码调整为如下所示,然后将“numbers.txt”移动到您的项目文件夹中。使用完整路径不是一个好习惯,因为每次切换到另一台计算机时都必须更改它。 将它放在 manifest.mf 文件附近。

import java.io.File;
import java.util.Scanner;

public class RangeAverage {

public static void main(String[] args) {
    try {
        int average = 0, num, total = 0;
        Scanner scan = new Scanner(new File("numbers.txt"));
        while(scan.hasNextLine())
        {
            num = scan.nextInt();
            if(num < 10 || num > 30)
            {
                average = average + num;
                total++;
            }
        }
        scan.close();
        System.out.println(" Average of numbers in range is " + (average/total));
    } catch (Exception e) {
        // print out the error
        System.out.println(e.toString());
    }
  }
}

确保每一行都有一个数字,并删除任何没有数字的新行!因为它将返回一个错误,除非您进行检查以检查该行是否为空。

【讨论】:

    猜你喜欢
    • 2022-08-17
    • 2018-11-02
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2021-12-09
    • 2016-01-10
    相关资源
    最近更新 更多