Java 在不使用数组的情况下从整数中获取最低和最高值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19435242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:07:45  来源:igfitidea点击:

Getting the lowest and highest value from integers without using arrays?

javaloopsinteger

提问by Dinco

I'm trying to write a class which reads 5 integers from the user and returns the highest and lowest value back. This must be done using loops and without using arrays and Integer.MIN.Value/Integer.MAX.Value. I've already succeeded writing code that gets 5 integers from the user and returns the highest value but I just can't get both the highest and the lowest value returned in the same class.

我正在尝试编写一个类,该类从用户读取 5 个整数并返回最高和最低值。这必须使用循环来完成,而不使用数组和 Integer.MIN.Value/Integer.MAX.Value。我已经成功编写了从用户那里获取 5 个整数并返回最高值的代码,但我无法同时获得同一类中返回的最高值和最低值。

Here is the code I mentioned above:

这是我上面提到的代码:

import java.util.Scanner;

    public class Ovning_321 {
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            int number;
            int max = 0;

            for (int x = 0; x<5; x++){ 
                 System.out.print("Give me an integer: "); 
                 number = input.nextInt(); 

                 if (number > max){ 
                      max = number;  
                 }              
             }                  
             System.out.println("Highest value: " + max);
     }
}

采纳答案by necromancer

here you go :)

干得好 :)

import java.util.Scanner;

    public class Ovning_321 {
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            int number;
            int max = 0;
            int min = 0;

                  for (int x = 0; x<5; x++){ 
                        System.out.print("Give me an integer: "); 
                        number = input.nextInt(); 

                        if (x == 0 || number > max){ 
                            max = number;  
                        }               
                        if (x == 0 || number < min){ 
                            min = number;  
                        }               
                  }                 
                  System.out.println("Highest value: " + max);
                  System.out.println("Lowest value: " + min);
            }
     }

回答by underbar

Why not just repeat your max logic for min?

为什么不重复你的最大逻辑最小值?

public class Ovning_321 {
    public static void main(String[] args){

        Scanner input = new Scanner(System.in);
        System.out.print("Give me an integer: "); 

        number = input.nextInt();
        int max = number;
        int min = number;

              for (int x = 0; x<4; x++){ 
                    System.out.print("Give me an integer: "); 
                    number = input.nextInt(); 

                    if (number > max){ 
                        max = number;  
                    }

                    if (number < min){ 
                        min = number;  
                    } 
              }                 
              System.out.println("Highest value: " + max);
              System.out.println("Lowest value: " + min);
        }
 }

Note that max and min are initially set to the first number that the user enters, so there will be no false 0's and no need to MAX_INTor MIN_INT. This in turn makes the loop run once less so terminate at i == 4instead of 5.

请注意, max 和 min 最初设置为用户输入的第一个数字,因此不会出现假 0,也不需要MAX_INTor MIN_INT。这反过来又使循环运行次数减少,因此终止于i == 4而不是 5。