简体   繁体   English

除了第一个和最后一个之外,我如何对数组中的所有项目求和

[英]How I can sum all items in array except first and last

I new for coding I want to know what my mistake is.我是编码新手,我想知道我的错误是什么。 The chart asking convert to double add to an array then sum all items in array except frist and last.图表要求转换为双加到数组,然后将数组中除第一个和最后一个之外的所有项目相加。 from text file从文本文件

this is the text file:这是文本文件:

8.7 6.5 0.1 3.2 5.7 9.9 8.3 6.5 6.5 1.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 8.7 6.5 0.1 3.2 5.7 9.9 8.3 6.5 6.5 1.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0

import java.io.*;
import java.util.*;
import java.text.*;
public class BaseClass
{
   public static void main(String args[]) throws IOException
   {
      NumberFormat fmt = NumberFormat.getNumberInstance();
      fmt.setMinimumFractionDigits(4);
      fmt.setMaximumFractionDigits(4);
      Scanner sf = new Scanner(new File("C:\\temp_Name\\DataGym.in.txt"));
      int maxIndx = -1;
      String text[] = new String[1000];

      while(sf.hasNext()) {
      maxIndx++;
      text[maxIndx] = sf.nextLine();
   }
   sf.close();
   int contestant = 0;

   for (int j = 0; j <= maxIndx; j++) {
     Scanner sc = new Scanner(text[j]);
     double sum = 0;
     double answer=0;
     double array[] = new double[1000];
     while(sc.hasNext()){
       Arrays.sort(array);
       double b=sc.nextDouble();  
     } 
     contestant++;
     answer = answer + sum;
     System.out.println("For Competitor #" + contestant + ", the average is " + (answer/8) );
   }
  }
}

I want to print something like:我想打印如下内容:

For Competitor #1, the average is 5.8625
For Competitor #2, the average is 0.0000
For Competitor #3, the average is 1.0000

在上面列出的代码中,您永远不会更改变量 sum 和 answer 的值,因此它们将始终为 0,就像在初始化语句中一样。

From what I understand of your question, you want to discard the best and worst result for the competitor, and then calculate the mean of the remaining numbers.根据我对您的问题的理解,您想丢弃竞争对手的最佳和最差结果,然后计算剩余数字的平均值。 At the moment you are not writing the values for each competitor into the array that you sort.目前您没有将每个竞争对手的值写入您排序的数组中。 You're almost there.您快到了。

You would find it easier using ArrayList and ArrayList rather than arrays of strings and doubles.您会发现使用 ArrayList 和 ArrayList 比使用字符串和双精度数组更容易。

For example, to read in the lines from your file:例如,要读入文件中的行:

File file = new File( "C:\\temp_Name\\DataGym.in.txt" );

ArrayList<String> lines = new ArrayList<String>();

// Use try() like this to automatically close the file when finished
try( Scanner scanner = new Scanner( file ) ) {
   while( scanner.hasNext() )
      lines.add( scanner.nextLine() );
}
catch( Exception ex ) {
   // TODO: handle a failure to read your input file gracefully...
}

Then to process the results for each competitor:然后处理每个竞争对手的结果:

for( String line : lines ) {
   Scanner scanner = new Scanner(line);
   ArrayList<Double> values = new ArrayList<Double>();
   while( scanner.hasNextDouble() )
      values.add( scanner.nextDouble() );

   Collections.sort(values);
   double sum = 0;

   // Sum starting from the second element, and finishing before the last
   for( int index = 1; index < values.size() - 1; ++index )
      sum += values.get(index);

   // TODO: What do you want to do if there are two or fewer values?
   double mean = sum / values.size() - 2;

   // Do whatever you want to log this contestants results...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM