-1

对不起,如果我打扰了。但我被困在我的硬件问题的另一部分。我现在的任务不是让销售人员 0,而是从 1 开始销售人员。我试图通过将字符串 i 解析为整数并加 1 来解决这个问题。似乎它会起作用。但是,由于某种原因,min 的计算不正确。它保持在 0。我尝试单步执行代码并查看原因,但我看不到它。

import java.util.Scanner;
public class Cray {
public static void main(String[] args){

    final int SALESPEOPLE = 5;
    int[] sales = new int[SALESPEOPLE];
    int sum, randomValue, numGreater = 0;
    int max = sales[0];
    int min = sales[0];
    int maxperson = 0;
    int minperson = 0;



    Scanner scan = new Scanner(System.in);
    for (int i=0; i<sales.length; i++)
        {
        //To attempt print out the information of salesperson 0 and salesperson 1, I returned the integer value of i and added one.
        System.out.print("Enter sales for salesperson " + Integer.valueOf(i+1) + ": ");
        sales[i] = scan.nextInt();

        //max if statemnent works fine and is correct
        if(sales[i] > max){
            max= sales[i];
            maxperson = i;
        }
        //For some reason max is calculating but not min. 
        if(sales[i] < min){
            min = sales [i];
            minperson= i;
        }


        }



    System.out.println("\nSalesperson   Sales");
    System.out.println("--------------------");
    sum = 0;
    for (int i=0; i<sales.length; i++)
        {
        System.out.println("     " + Integer.valueOf(i+1) + "         " + sales[i]);
        sum += sales[i];
        }

    System.out.println("\nTotal sales: " + sum);
    System.out.println("Average sales: " + sum/5);
    System.out.println();
    System.out.println("Salesperson " + Integer.valueOf(maxperson+1) + " had the most sales with " + max );
    System.out.println("Salesperson " + Integer.valueOf(minperson+1) + " had the least sales with " + min);
    System.out.println();
    System.out.println("Enter any value to compare to the sales team: ");
    randomValue = scan.nextInt();
    System.out.println();
    for(int r=0; r < sales.length; r++){
        if(sales[r] > randomValue){
            numGreater++;
            System.out.println("Salesperson " + Integer.valueOf(r+1) + " exceeded that amount with " + sales[r]);
        }

    }
    System.out.println();
    System.out.println("In total, " + numGreater + " people exceeded that value");

      }

    }
4

1 回答 1

2

The problem is that the min is never set as all positive sales will be > 0

You should initialise min as a large value so that a positive sales figure can be less then the minimum value specified in your if statement:

if (sales[i] < min) {

You could use:

int min = Integer.MAX_VALUE;
于 2012-10-20T18:53:57.563 回答