简体   繁体   English

确定最高和最低编号

[英]Determine the highest and lowest number

I'm currently writing a program where the user must input 10 numbers and then the output will be the highest number and the lowest number. 我当前正在编写一个程序,其中用户必须输入10个数字,然后输出将是最高数字和最低数字。 There is something wrong in my code but couldn't find it. 我的代码中有错误,但找不到。

int highest=0, lowest=0, num=0;
Scanner scan = new Scanner(System.in);

for (int i=0; i<10; i++) {
    System.out.print("Enter a number:");
    num = scan.nextInt();
}

if (num > highest) {           
    highest = num;
}         
else if(num < lowest) {             
    lowest = num;
}

System.out.println("Highest number is: " + highest);
System.out.println("Lowest number is: " + lowest);

Initialise your values differently: 以不同的方式初始化值:

int highest = Integer.MIN_VALUE;
int lowest = Integer.MAX_VALUE;

If you initialise them both to zero, you will never have a "highest" value below zero, or a "lowest" value above zero 如果将它们都初始化为零,则永远不会有小于零的“最高”值或大于零的“最低”值

You should put your two if conditions in the for loop else you will only compare the last number. 您应该将两个if条件放入for循环中,否则只会比较最后一个数字。 And lowest shouldn't be set to 0 but to Integer.MAX_VALUE 并且最低值不应设置为0,而应设置为Integer.MAX_VALUE

You have some issues with your initialization and your logic: 您的初始化和逻辑存在一些问题:

int highest=Math.MIN_VALUE;
int lowest=Math.MAX_VALUE;
int num=0;
Scanner scan = new Scanner(System.in);


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

   System.out.print("Enter a number:");
   num = scan.nextInt();
   if (num > highest){

    highest = num;
   }

   if(num < lowest){

    lowest = num;
   }

}



   System.out.println("Highest number is: " + highest);
   System.out.println("Lowest number is: " + lowest);

You should also use 2 if conditions rather than an else if . 您还应该使用if条件,而不是else if If you have only one number, chances are that you will end up with something similar to highest being equal to some digit you entered while lowest will still be equal to Math.MAX_VALUE . 如果您只有一个数字,则很可能会得到类似的结果,即highest等于您输入的某个数字,而lowest仍等于Math.MAX_VALUE This can cause confusion. 这会引起混乱。

You are implicitly assuming that lowest and largest are 0, that might now be the case, try this code snippet.. 您隐式地假设最低和最大为0,现在可能是这种情况,请尝试以下代码段。

class Main{
        public static void main(String args[]){
                int highest=0, lowest=0, num=0;
                Scanner scan = new Scanner(System.in);
                highest = lowest = scan.nextInt();
                for(int i=1; i<10; i++){
                       System.out.print("Enter a number:");
                       num = scan.nextInt();
                       if (num > highest){
                           highest = num;
                       }
                       if(num < lowest){
                           lowest = num;
                    }
                    System.out.println("Highest number is: " + highest);
                    System.out.println("Lowest number is: " + lowest);
               }
        }
}

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

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