Java使用循环,接受五个整数,从控制台输入并返回min&最大

时间:2018-02-11 05:07:47

标签: java loops console max min

我正在尝试使用循环创建Java代码,该循环将接受来自控制台的五个整数输入并返回最小和最大。我遇到了代码聚集的问题。谢谢你

import java.util.*;
public class Highest_LowestofFiveIntegers {

public static void main(String[] args) {

    System.out.print("Enter five integers separated by a 
          space i.e. 2 6 55 1 3 910: "); 
    int number = 0;

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

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


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

}

}

1 个答案:

答案 0 :(得分:2)

number = input.nextInt(); 放入 for循环中;它之前

int max = 0, min = 0;
for (int x = 0; x < 5; x++) { 
    int 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);