-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,因为您可以从行中读取值

你也不需要for在你的循环中有一个嵌套循环while,每一行都被while循环读取一次,所以基本上在你的 while 循环中这样做

  1. 读取值
  2. 添加到数组(使用ArrayListso 可以具有动态长度)
  3. 加起来
  4. 比较是否最便宜
于 2013-08-01T02:44:05.343 回答
0

尝试这个,

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

代替

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

因为,您必须逐行读取文件。

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 回答