在找到“n”个用户输入数的平均值时尝试查找Min& max时出错

时间:2017-08-17 00:55:19

标签: java loops average minmax

package HW2_Min_Max;

import java.util.Scanner; 

public class HW2_Min_Max {

    public static void main(String[] args) {

        Scanner myScanner = new Scanner(System.in);

        System.out.println("Please input a positive interger that indicates number of positive intergers ");

        int number = myScanner.nextInt();

        while (number <= 0) {
            System.out.println("Please input interger");
            number = myScanner.nextInt();
        }

        int i=1; //i is to store current iteration
        int sum=0; //sum is to store sum of the input 
        int x; //x is to store the user input

        while (i <= number){
            System.out.println("Please input a positive interger ");
            x = myScanner.nextInt();
            sum = sum + x;
            i++;
        }
        int average = sum/number;

        System.out.println("The average is " + average);

        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;

        if (number < min){
            min = number;
        }

        if (number > max) {
            max = number;
        }
        System.out.println("The minimum value is " + min);
        System.out.print( "and the maximum value is" + max);
        }
    }
}

1. ^这是我遇到问题的地方,在Netbeans的最后一个支架上我得到一个错误,说“类,接口或枚举预期”,但我不明白为什么。请原谅我的无知,因为我是一个非常新鲜的java初学者,更不用说编程了。

2 个答案:

答案 0 :(得分:0)

对于第一部分,我建议您使用do-while循环来测试数字的数量。像,

int number;
do {
    System.out.println("Please input a positive integer that "
            + "indicates number of positive integers ");
    number = myScanner.nextInt();
} while (number <= 0);

然后,您需要在循环中设置minmax (或存储您阅读的所有值)。我希望Math.maxMath.min。我也会从0算起。像,

int i = 0; // i is to store current iteration
int sum = 0; // sum is to store sum of the input
int x; // x is to store the user input
int max = Integer.MIN_VALUE; // store the max user input
int min = Integer.MAX_VALUE; // store the min user input
while (i < number) {
    System.out.println("Please input a positive integer ");
    x = myScanner.nextInt();
    if (x > 0) { // make sure it's a positive integer
        min = Math.min(min, x);
        max = Math.max(max, x);
        sum += x;
        i++;
    }
}
int average = sum / number;
System.out.println("The average is " + average);
System.out.println("The minimum value is " + min);
System.out.println("and the maximum value is " + max);

答案 1 :(得分:-1)

现在工作正常。我已经做了一些改变。请考虑一下。我希望它会有所帮助。

 Scanner myScanner = new Scanner(System.in);

    System.out.println("Please input a positive interger that indicates number of positive intergers ");


     int number = myScanner.nextInt();

    while (number <= 0) {
        System.out.println("Please input interger");
        number = myScanner.nextInt();
    }
    int[] arr = new int[number]; // Store values in array
    int i=0; //i is to store current iteration
    int sum=0; //sum is to store sum of the input
    int x; //x is to store the user input
    int max = 0;
    int min = 0;
       for(i=0;i<number;i++){
        System.out.println("Please input a positive interger ");
        arr[i] = myScanner.nextInt();
        sum = sum + arr[i];

    }
    int average = sum/number;

    System.out.println("The average is " + average);

    // Put initial values in min and max for comparing
    min =arr[0];
     max = arr[0];
for(i=1;i<number;i++)
{
    // Compare if values is less than arr[i]
    if (arr[i] < min){
        min = arr[i];
    }
// Compare if values is greater than arr[i]
    if (arr[i] > max) {
        max = arr[i];
    }
}
    System.out.print("The minimum value is " + min);
    System.out.println( " and the maximum value is " + max);

}
}

这是输出:

enter image description here