简体   繁体   中英

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

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.

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...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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