【问题标题】:Error with trying to find Min&max when also finding average of "n" number of user inputs在查找“n”个用户输入的平均值时尝试查找最小值和最大值时出错
【发布时间】:2017-08-17 00:55:18
【问题描述】:
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 的新手,更不用说编程了。

【问题讨论】:

  • 看起来最后一个大括号实际上根本不需要。请记住{ 的数量应该等于} 的数量,您要确保打开的每个大括号最终都关闭。
  • 正确、一致的缩进是你的朋友。
  • 好的,这样就解决了让我运行代码的问题,但是我的实际输出有问题?我输入了5个正整数,平均值总是正确的,但是max和min的输出错误,我不知道为什么?
  • 不要在 cmets 中重新定义您的问题。如果您还有其他问题,请单独提出。并包含更多细节。
  • 不是直接针对您的问题,但“整数”这个词是正确的“整数”,如java.lang.Integer

标签: java loops average minmax


【解决方案1】:

对于第一部分,我建议您使用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);

【讨论】:

    【解决方案2】:

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

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

    这是输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 2015-01-16
      • 1970-01-01
      • 2014-06-11
      • 2016-01-05
      相关资源
      最近更新 更多