在NumberAnalyzer.java上获得0.0。需要帮助

时间:2016-11-11 01:48:50

标签: 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.*;    // 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。请给我一些建议。谢谢!

2 个答案:

答案 0 :(得分:0)

溶液

改变你的方法

private void getNumbers(String filename)

public void getNumbers()

然后再做

NumberAnalyzer na = new NumberAnalyzer("Numbers.txt");
na.getNumbers();

答案 1 :(得分:0)

您没有调用getNumbers()方法,因此未填充numbers[]数组。

只需在构造函数中进行调用,如下所示:

public NumberAnalyzer(String filename) throws IOException
        {
            count = 0;
            file = new File("Numbers.txt");
            scan = new Scanner(file);
            numbers = new double[11];
            getNumbers();
        }

看起来它不需要有String参数,所以你可以删除它,除非你仍然计划用它实现一些东西。