-2

これが私のコードです。各トークンを適切にトークン化し、ループの各サイクルでそれらを配列に配置する方法と、配列の合計を取得する方法と最も遠い値を取得する方法について助けが必要ですか?

 import java.io.*;
 import java.util.*;

public class Data{ 
public static void main ( String[] args ) throws IOException{ 
  String Filename = "Data.txt" ; 
    String line;

      FileReader Filereader = new FileReader(Filename);
      BufferedReader input = new BufferedReader(Filereader);
      line = input.readLine(); 

      System.out.println("--- oOo ---");
      System.out.println("AVERAGE ACID LEVEL");
      System.out.println("--------------------------------------------");

        double[] nums = new double[13];
        int sum = 0;

      while ( line != null ) // continue until end of file 
      { 

        StringTokenizer token = new StringTokenizer(line);



            for ( int i = 0; i < nums.length; i++ )
           {

              String temp = input.readLine();
              nums[i] = Double.parseDouble(temp);

              System.out.println(nums[i]);
           }

      } 
      input.close(); 

} 
    }

おー!ここに data.txt のデータがあります

5.6
6.2
6.0
5.5
5.7
6.1
7.4
5.5
5.5
6.3
6.4
4.0
6.9

どんな助けでも大歓迎です...ありがとう

4

2 に答える 2

3

データ値は毎回新しい行にあるためStringTokenizer、行から値を読み取ることができるので必要ありません

また、ループにネストされたループを含める必要はありませんforwhile各行はwhileループによって一度読み取られるため、基本的にwhileループ内でこれを行います

  1. 読み取り値
  2. 配列に追加 (ArrayList動的な長さを持つことができるように使用)
  3. 合計に追加
  4. 一番遠いか比較
于 2013-08-01T02:44:05.343 に答える
0

これを試して、

while ((line = input.readLine()) != null)

それ以外の

line = input.readLine(); // it having the first value

ファイルを 1 行ずつ読み取る必要があるためです。

while ( line != null ) // so only your loop is unbreakable

コピーして貼り付けないでください。理解しようとする。

while ((line = input.readLine()) != null) // This will read the file line by line till last value.
        {
            values[i] = Double.valueOf(line); 
            i++;                          // This is for finding the total number of values from the file.
        }

        Double sampleInput = 0.0;
        for(Double valueArray : values)
        {
            sampleInput = sampleInput + valueArray; // Atlast we sum all the array values.
        }

        Double output = (double) sampleInput/values.length;
于 2013-08-01T02:58:49.447 に答える