简体   繁体   English

使用for循环查找最大值和最小值

[英]Find a maximum and minimum value using for loop

I am trying to take 10 integers from the user's input and find the minimum and maximum using for loop. 我试图从用户的输入中获取10个整数,并使用for循环找到最小值和最大值。 But my final print statement just prints the list of numbers entered. 但是我的最终打印语句只是打印输入的数字列表。 I'm lost. 我迷路了。

public static void main(String[]args) {

   Scanner scan=new Scanner(System.in);
   double a = 0;
   double max = 0;
   double min = 0;

   System.out.print("Enter ten floating points: \n");

   for(a=0; a <10; a++) {
      a=scan.nextDouble();

       if(a == 0) { 
           min=a;
           max=a; 
       } 
       else if(a < min) {
           min=a; 
       }
       else if (a > max){ 
           max=a; 
       }
   }
   System.out.println("Minimum value: " +min);
   System.out.println("Maximum value: " +max);
}

Issue is in your for loop change it to 问题在您的for循环中将其更改为

for (int x = 0; x < 10; x++) {

there is another issue, you need to change 还有另一个问题,你需要改变

if(a == 0){

To

if (x == 0) {

First your code should not run correctly since you use the same variable a as the counter and as the variable to store user input. 首先,由于您使用与计数器相同的变量a和存储用户输入的变量,因此代码不应正确运行。 You should use two different variable. 您应该使用两个不同的变量。

Second declare your variable that store the input from user inside the loop, otherwise it may keep the value from the previous loop. 第二个声明您的变量,该变量将用户的输入存储在循环内,否则它可能会保留上一个循环中的值。

Third your if(a == 0) condition will reset min and max when the user enter the number 0. Which is not what you want. 第三,当用户输入数字0时, if(a == 0)条件将重置minmax 。这不是您想要的。

Finally you should not initialize max/min like that. 最后,您不应该像这样初始化max / min。 By defining min as 0, if the user enter only positive number the min will be 0 but the user never entered 0. You instead initialize them at the first entry from user. 通过将min定义为0,如果用户仅输入正数,则min将为0,但用户从未输入0。您可以在用户的​​第一项输入中将其初始化。

This should look like this : 看起来应该像这样:

public static void main(String[]args) {

    Scanner scan=new Scanner(System.in);

    System.out.print("Enter ten floating points: \n");
    double tmp = scan.nextDouble(); //read first number from user

    double max = tmp; //intialize with the first input
    double min = tmp;

    for(int i=0; i <9; i++) { //from 0 to 8, 9 numbers since the first has already been read
        double a = scan.nextDouble(); //at every loop read a number from the input
        if(a < min) {
            min=a;
        }
        //removed else since max and min are independant
        if (a > max) {
            max=a;
        }
    }
    System.out.println("Minimum value: " +min);
    System.out.println("Maximum value: " +max);
}

Try this 尝试这个

    Scanner scan=new Scanner(System.in);
    int maximum = Integer.MIN_VALUE;
    int minimum = Integer.MAX_VALUE;
    for( int i=0; i<10 &&  scan.hasNextInt(); i++ ) {
        int next = scan.nextInt();
        maximum = Math.max( next, maximum);
        minimum = Math.min( next, minimum);
    }
    System.out.println("Found maximum :"+maximum+", minimum:"+minimum);
    scan.close();

First, we create the scanner. 首先,我们创建扫描仪。 Next, we set a value for your maximum - since integers can be negative, we can not use 0, but have to use the smallest possible integer. 接下来,我们为您的最大值设置一个值-由于整数可以为负,因此我们不能使用0,而必须使用最小的整数。 Same for minimum. 最低限度相同。

In the for loop, we have to make sure that we terminate the loop after 10 iterations, or if the input stream does not have any more int's. 在for循环中,我们必须确保在10次迭代后终止输入循环,或者如果输入流中不再有int循环,则终止循环。

Next, we use the mathematical function max to find out which number is largest - the previously found maximum, or the next int from the Scanner. 接下来,我们使用数学函数max找出最大的数字-先前找到的最大数,或Scanner中的下一个整数。 And same for minimum. 最少也一样。

Finally, remeber to close the Scanner, to avoid resource leakage. 最后,请记住关闭扫描仪,以避免资源泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM