简体   繁体   中英

Getting min and max values from an array - Java

import java.util.Scanner;

public class Sales {
  public static void main(String[] args) {
    int[] sales;
    sales = getSales();
    printSales(sales);
    printSummary(sales);
  }

  private static int[] getSales() {
    Scanner input = new Scanner(System.in);
    int[] temp;
    System.out.print("Enter the number of salespeople: ");
    temp = new int[input.nextInt()];                                      

    for (int i = 0; i < temp.length; i++) {
      System.out.print("Enter sales for salesperson " +
                       (i + 1) + ": ");
      temp[i] = input.nextInt();                                              
    }
    return temp;                                                      
  }

  private static void printSales(int[] s) {
    System.out.println();
    System.out.println("Salesperson   Sales");
    System.out.println("-----------   -----");
    for (int i = 0; i < s.length; i++) {
      System.out.printf("%6d%12d\n", i + 1, s[i]);                     
    }
  }

  private static void printSummary(int[] s) {
    int sum      = 0;
    int max_sale = 0;  // Salesperson with the most sales
    int min_sale = 0;  // Salesperson with the least sales

    for (int i = 0; i < s.length; i++)  {
      sum = (s[i] + sum);                                          
      if (s[i] > max_sale)
        max_sale = s[1];
      else if (s[i] > min_sale)
        s[i] = min_sale;   
    }
    System.out.println();
    System.out.println("Total sales:  " + sum);
    System.out.println("Average sales: " + (double)sum / s.length);
    System.out.println("Salesperson " + (max_sale + 1) +
                       " had the maximum sale with "   +
                       s[max_sale]);
    System.out.println("Salesperson " + (min_sale + 1) +
                       " had the minimum sale with "   +
                       s[min_sale]);
  }
}

The purpose of the application is to take the number of salespeople as input, along with their sales and then display individual sales, total sales, and average. That is working fine, but it's also supposed to display which salesperson had the max and minimum sales and what they were (lines 51 - 54). At the moment, any time the max is greater than the number of salespeople I get an ArrayIndexOutOfBoundsException and for whatever reason can't figure it out.

1 - Modify your for loop to get the max and min without modifying the array
2 - Try to print max and min instead of printing sum[max] and some[min] (which can throws IndexOutOfBoundsException ) 3 - min_sale should be greater than 0, actually a value enough large (because you can have only positive values in your array)

To summarize :

    int sum      = 0;
    int max_sale = Integer.MIN_VALUE;  // Salesperson with the most sales
    int min_sale = Integer.MAX_VALUE;  // Salesperson with the least sales

    for (int i = 0; i < s.length; i++){
          sum = (s[i] + sum);                                          
          if (s[i] > max_sale)
            max_sale = s[i];
          else if (s[i] < min_sale)
            min_sale = s[i];   
    }

System.out.println("Salesperson " +
                       " had the maximum sale with "   +
                       max_sale);
System.out.println("Salesperson " +
                       " had the minimum sale with "   +
                       min_sale);

the specific problem that's causing your error is here,

System.out.println("Salesperson " + (max_sale + 1) +
                   " had the maximum sale with "   +
                   s[max_sale]);
System.out.println("Salesperson " + (min_sale + 1) +
                   " had the minimum sale with "   +
                   s[min_sale]);

you're using your result as though it were an index

change it to the following

System.out.println("Salesperson " + (max_sale + 1) +
                   " had the maximum sale with "   +
                   max_sale);
System.out.println("Salesperson " + (min_sale + 1) +
                   " had the minimum sale with "   +
                   min_sale);
 if (s[i] > max_sale)
    max_sale = s[1];
  else if (s[i] > min_sale)
    s[i] = min_sale; 

Here you are trying to assign the value in s[1] to max_sales. whereas you should be assigning max_sale = i . and the if condition should be

if(s[i] > s[max_sale] )

Updated code:

for (int i = 0; i < s.length; i++)                                    
{
  sum = (s[i] + sum);                                          
  // Finds the index of the sales person with best sales
  if (s[i] >= s[max_sale])
    max_sale = i;
  // If this sales person is not the best, then check if he is last
  else if (s[min_sale] > s[i])
    min_sale = i;   
}

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