在NumberAnalyzer.java上获取0.0。需要帮助吗
使用接受文件名作为参数的构造函数编写一个类。假设文件包含一系列数字,每个数字都写在单独的一行上。类应该将文件的内容读入数组,然后显示以下数据 数组中的最小数 数组中的最大数 数组中数字的总数 数组中数字的平均值。 用于上述程序的文件Numbers.txt包含以下十二个数字:在NumberAnalyzer.java上获取0.0。需要帮助吗,java,arrays,Java,Arrays,使用接受文件名作为参数的构造函数编写一个类。假设文件包含一系列数字,每个数字都写在单独的一行上。类应该将文件的内容读入数组,然后显示以下数据 数组中的最小数 数组中的最大数 数组中数字的总数 数组中数字的平均值。 用于上述程序的文件Numbers.txt包含以下十二个数字: 8.71 7.94 3.01 29.27 9.23 82.76 12.6 47.99 63.89 1.09 22.23 79.17 这是主程序:NumberAnalyzerDemo.java import java.io.
8.71
7.94
3.01
29.27
9.23
82.76
12.6
47.99
63.89
1.09
22.23
79.17
这是主程序:NumberAnalyzerDemo.java
import java.io.*; // Needed for IOException
/**
This program demonstrates a solution to the
Number Analysis Class programming challenge.
*/
public class NumberAnalyzerDemo
{
public static void main(String[] args) throws IOException
{
// Create a NumberAnalyzer object.
NumberAnalyzer na = new NumberAnalyzer("Numbers.txt");
// Display data about the numbers in the file.
System.out.println("The lowest number in the file is " +
na.getLowest());
System.out.println("The highest number in the file is " +
na.getHighest());
System.out.println("The total of the numbers in the file is " +
na.getTotal());
System.out.println("The average of the numbers in the file is " +
na.getAverage());
}
}
这是一个类:NumberAnalyzer.java
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
/**
The NumberAnalyzer class is to searching the numbers in a file.
*/
public class NumberAnalyzer
{
private double[] numbers;
private int count;
File file;
Scanner scan;
/**
Constructer that accepts the file name as its argument.
*/
public NumberAnalyzer(String filename) throws IOException
{
count = 0;
file = new File("Numbers.txt");
scan = new Scanner(file);
numbers = new double[11];
}
/**
The getLowest() method to search the file and pull out the lowest
number in the file.
@return Return the lowest number.
*/
public double getLowest()
{
double low = numbers[0];
for (int i = 0; i < numbers.length; i++)
{
if (low > numbers[i])
{
low = numbers[i];
}
}
return low;
}
/**
The getHighest() method to search the file and pull out the highest
number in the file.
@return Return the highest number.
*/
public double getHighest()
{
double high = numbers[0];
for (int i = 0; i < numbers.length; i++)
{
if (high < numbers[i])
{
high = numbers[i];
}
}
return high;
}
/**
This method calculate the total of all the number in the file.
@return Adding all number in the file.
*/
public double getTotal()
{
double total = 0;
for (int i = 0; i < numbers.length; i++)
{
total += numbers[i];
}
return total;
}
/**
This method used to calculate the average of the numbers in the file.
@return Using the getTotal() divided to the length of the numbers.
*/
public double getAverage()
{
return getTotal() / numbers.length;
}
/**
This method to read all the file txt and get the right number.
*/
private void getNumbers(String filename)
{
while(scan.hasNext())
{
numbers[count] = scan.nextDouble();
count++;
}
scan.close();
}
/**
This method
*/
private int getNumberOfValues(String filename)
{
return count ;
}
}
所有的输出都是0.0。请给我一些建议。谢谢 解决方案
改变你的方法
private void getNumbers(String filename)
到
然后呢
NumberAnalyzer na = new NumberAnalyzer("Numbers.txt");
na.getNumbers();
您没有调用getNumbers方法,因此没有填充numbers[]数组
只需在构造函数中进行调用,如下所示:
public NumberAnalyzer(String filename) throws IOException
{
count = 0;
file = new File("Numbers.txt");
scan = new Scanner(file);
numbers = new double[11];
getNumbers();
}
看起来它不需要字符串参数,所以您可以删除它,除非您仍打算用它实现某些功能。您是否尝试过自己测试它?在代码中添加一些print语句。它是否成功读入了号码?把它们加起来?在期望我们为您完成工作之前,请尝试找出哪个步骤失败。是的,哪个步骤失败?在哪里调用getNumbers方法?这是读取数字的步骤。你有没有调用过getNumbers?或者我只是错过了它?我认为getNumberOfValues方法需要一些更改。这是一个错误,先生!java:14:错误:类NumberAnalyzer中的方法getNumbers不能应用于给定类型;na.getNumbers;^必需:找到字符串:无参数原因:实际参数列表和形式参数列表长度不同修复了此错误。Error表示getNumbers是私有的,但当我更改为public时,它仍然显示Error.Exception在线程main java.lang.ArrayIndexOutOfBoundsException:11在NumberAnalyzer.GetNumberNumberNumberAnalyzer.java:99在NumberAnalyzerDemo.mainNumberAnalyzerDemo.java:14您正在读取12个数字,但在数组中只分配了11个。在读取每个数字时,需要分配12或使用ArrayList并添加或更好地计算统计数据。@NhokV请查看上面JasonM1的评论。
public NumberAnalyzer(String filename) throws IOException
{
count = 0;
file = new File("Numbers.txt");
scan = new Scanner(file);
numbers = new double[11];
getNumbers();
}