简体   繁体   English

从数组获取最小值和最大值-Java

[英]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). 这样做很好,但是还应该显示哪个销售员的最大和最小销售额以及销售额是多少(第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. 此刻,任何时候最大数量都大于我得到ArrayIndexOutOfBoundsException的销售人员的数量,无论出于什么原因都ArrayIndexOutOfBoundsException

1 - Modify your for loop to get the max and min without modifying the array 1-修改您的for循环以获取最大值和最小值,而无需修改数组
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) 2-尝试打印最大值和最小值,而不是打印sum[max]some[min] (这可能会引发IndexOutOfBoundsException )3- min_sale应该大于0,实际上是一个足够大的值(因为您的正数只能为正值)数组)

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. 在这里,您尝试将s [1]中的值分配给max_sales。 whereas you should be assigning max_sale = i . 而您应该分配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;   
}

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

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