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

該應用程序的目的是將銷售人員的人數以及他們的銷售額作為輸入,然后顯示單個銷售額,總銷售額和平均值。 這樣做很好,但是還應該顯示哪個銷售員的最大和最小銷售額以及銷售額是多少(第51-54行)。 此刻,任何時候最大數量都大於我得到ArrayIndexOutOfBoundsException的銷售人員的數量,無論出於什么原因都ArrayIndexOutOfBoundsException

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

導致您的錯誤的特定問題在這里,

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);
 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(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;   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM