Java 确定最大/最小数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20469170/
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-13 02:10:22  来源:igfitidea点击:

Determine largest/smallest number

javaalgorithmlogic

提问by user3081689

im trying to code a java program that gets 5 numbers from user and determines which is the largest and which is the smallest, i got a problem with the logic , i cant quiet figure how to compare all 5 input numbers with one another and determine the largest and smallest :

我试图编写一个 java 程序,它从用户那里获取 5 个数字并确定哪个是最大的,哪个是最小的,我的逻辑有问题,我无法安静地弄清楚如何将所有 5 个输入数字相互比较并确定最大和最小:

import java.util.Scanner;
    public class determine {

    public static void main (String [] args) {

        Scanner scan=new Scanner(System.in);


            System.out.println("enter 5 numbers : ");
            for(int i=1; i<=5; i++){
            int num=scan.nextInt();

            if (num is bigger than the rest)

                System.out.println(num+" is the largest");

                if (num is smaller than rest )

                    System.out.println(num+" is the smallest");



        }


    }
    }

i sorted out the problem , is there any other way around for comparing the numbers rather than setting a value for min and max ?

我解决了这个问题,有没有其他方法可以比较数字而不是设置 min 和 max 的值?

import java.util.Scanner;
    public class determine {

    public static void main (String [] args) {

            int max=-9999;
            int min=9999;

        Scanner scan=new Scanner(System.in);


            System.out.println("enter 5 numbers : ");
            for(int i=1; i<=5; i++){
            int num=scan.nextInt();


                    if (num>max){max=num;}
                    if(num<min){min=num;}       
            }   

                        System.out.println(max+"largest");  
                        System.out.println(min+"smallest");


        }


    }

回答by Blub

Just use an extra variable maxin which you store the largest number you have seen so far. And for each new number you just check if the new number is larger than max, then you assign it to max. At the end you will have your maximum in max;-)

只需使用一个额外的变量max,您可以在其中存储迄今为止看到的最大数字。对于每个新数字,您只需检查新数字是否大于max,然后将其分配给max。最后,您将获得最大值max;-)

回答by user2336315

Just initialize min and max values. Then at each iteration, you'll have to make some tests on the number entered by the user and update accordingly min and max. I left the things to complete with ???.

只需初始化最小值和最大值。然后在每次迭代中,您必须对用户输入的数字进行一些测试,并相应地更新最小值和最大值。我留下了要完成的事情???。

                int max = Integer.MIN_VALUE;
                int min = Integer.MAX_VALUE;
                System.out.println("enter 5 numbers : ");
                for(int i=1; i<=5; i++){
                    int num=scan.nextInt();

                    if (?????)
                         max = ????;
                    else if (?????)
                         min = ????;
                }
                System.out.println(max+" is the largest");
                System.out.println(min+" is the smallest");

回答by user3020241

Create an array

创建数组

int[] numbers = new int[5];
for(int i=0; i<5; i++){
numbers[i]=scan.nextInt();
}

Then sort the array;

然后对数组进行排序;

int max=numbers[0]; 

for (int i=0; i<5; i++){
if (max<num[i]){
max=num[i]
}
}

System.out.println(max+" is the largest");

回答by Thufir

Here's the general idea:

这是一般的想法:

package max;

public class Max {
    //assumes integers
    //wrong output for min

    public static void main(String[] args) {
        int[] numbers = {99, 9, 502, 7, 3};
        int indexOfMin = 0;
        int indexOfMax = 0;
        int currentNumber = numbers[0];
        int min = numbers[0];
        int max = numbers[0];
        for (int i = 0; i < numbers.length; i++) {
            currentNumber = numbers[i];
            if (currentNumber >= max) {
                max = currentNumber;
                indexOfMax = i;
            }
            System.out.println("position " + i + " has a value of " + currentNumber);
            System.out.println("current max of " + max + " is at position " + indexOfMax);
        }
        System.out.println("max value of " + max + " is at position " + indexOfMax);
        System.out.println("min value of " + min + " is at position " + indexOfMin); //whoops, wrong output
    }
}

I'll leave the user input/output to you. As food for thought, after you do this, think about how to sort the array. Don't just look up the answer, try to sort it on your own. When posting homework, make it clear what you're doing and what you're asking for.

我会把用户输入/输出留给你。作为思考的食物,在你这样做之后,想想如何对数组进行排序。不要只查找答案,尝试自己排序。在发布家庭作业时,要明确你在做什么以及你要求什么。