0
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]);
  }
}

该应用程序的目的是将销售人员的数量作为输入,连同他们的销售额,然后显示个人销售额、总销售额和平均销售额。这很好用,但它也应该显示哪个销售人员的销售额最高和最低以及他们是什么(第 51 - 54 行)。目前,任何时候最大值大于我得到的销售人员的数量,ArrayIndexOutOfBoundsException无论出于何种原因都无法弄清楚。

4

3 回答 3

1

1 - 在不修改数组的情况下修改你的 for 循环以获得最大值和最小值
2 - 尝试打印最大值和最小值而不是打印sum[max]some[min](可以抛出IndexOutOfBoundsException) 3 -min_sale应该大于 0,实际上是一个足够大的值(因为你可以数组中只有正值)

总结一下:

    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);
于 2013-10-23T20:06:54.303 回答
0

导致您的错误的具体问题在这里,

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]);

你正在使用你的结果,就好像它是一个索引

将其更改为以下

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);
于 2013-10-23T20:06:39.810 回答
0
 if (s[i] > max_sale)
    max_sale = s[1];
  else if (s[i] > min_sale)
    s[i] = min_sale; 

在这里,您尝试将 s[1] 中的值分配给 max_sales。而你应该分配max_sale = i. 并且 if 条件应该是

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

更新代码:

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;   
}
于 2013-10-23T20:12:18.163 回答