在不使用数组的情况下从整数中获取最低和最高值? [英] Getting the lowest and highest value from integers without using arrays?

查看:120
本文介绍了在不使用数组的情况下从整数中获取最低和最高值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

以下是我上面提到的代码:

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


推荐答案

这里你去:)

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

这篇关于在不使用数组的情况下从整数中获取最低和最高值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆