ファイル名を引数として受け入れるコンストラクタでクラスを作成します。ファイルに一連の番号が含まれていて、それぞれが別の行に書き込まれているとします。クラスはファイルの内容を配列に読み込み、次のデータを表示する必要があります。NumberAnalyzer.javaで0.0を取得しています。助けが必要です
- アレイ
- アレイ
- アレイ内の数値の平均数の合計アレイ
- 最高数の最小数。
Numbers.txtは、上記のプログラムのために使用されるファイルは、これらの12個の数字が含まれています
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です。私にいくつかの提案をお願いします。ありがとう!
あなた自身でこれをテストしようとしましたか?いくつかのprintステートメントをコードに追加します。それは正常に番号で読んでいますか?それらを追加しますか?私たちがあなたのためにあなたの仕事をすることを期待する前に、どのステップが失敗しているかを調べてみてください。 – nhouser9
はい、どのステップが失敗していますか? –
getNumbers()メソッドはどこで呼び出されていますか?それは数字を読み取るステップです。 – JasonM1