java - Finding the min max and average of an array -
I need to find the minimum, maximum and average of balance. I have done this before using for loops, but Ever with a late loop Is there a way to pull the maximum max and average without interfering with the minimum loop?
10 Helen 1000 Jordan 755 EV 2500 Ken 80 Andrew 99 9 17 17 David 1743 Amy 12 Shaun 98 Patrick 7 Joy 14 Where the 10 accounts of The number is import java.util. *; Import java.io * *; Import java.util.Arrays; Public square bank countmen {public static zero main (string [] args) filenotfound exception {scanner infix = faucet; {InFile = Try New Scanner (New File ("account.txt")); ; } Hold (FileNotFoundException e) {System.out.println ("File not found!"); System.exit (0); } Int count = 0; Int accounts = inFile.nextInt (); String [] name = new string [accounts]; Int [] Balance = new int [accounts]; While (inFile.hasNextInt ()) {inFile.next (); Name [count] = inFile.next (); InFile.nextInt (); Balance [count] = inFile.nextInt (); Count ++; } System.out.println (Arrays.toString (name)); Println (Arrays.toString (balance)); }}
When you store these files, Drag the values balance [count] . You can then use those values that you have read to calculate: int min = Integer.MAX_VALUE; Int max = 0; Integer = 0; While (inFile.hasNext ()) {name [count] = inFile.next (); Balance [count] = inFile.nextInt (); // Balance [count] is now accumulating value from file if (balance [calculation] Max) {// Maximum = Balance [Calculation]; } Total + = balance [count]; // and count it ++; } Double average = (double) total / count;
Comments
Post a Comment