java 使用java找到两个最小的数字?

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

Find two smallest numbers using java?

java

提问by Karan Singh

I need help to compute the two smallest numbers in java?... plzz help.. Usage for arraylist and arrays are not allowed.. The user shall be asked for a terminating value first and will continue to provide inputs and once the user enters the same terminating value the program should output the two smallest numbers from the inputs excluding the terminating value..

我需要帮助来计算 java 中的两个最小数字?... plzz help.. 不允许用于数组列表和数组.. 应首先要求用户提供终止值,并在用户输入后继续提供输入相同的终止值程序应该从输入中输出两个最小的数字,不包括终止值..

Additional Details Could show me how to do it... The sentinel value is the value if entered again by the user shall stop the program and output the two smallest numbers.

其他细节可以告诉我怎么做... 哨兵值是用户再次输入的值,应停止程序并输出两个最小的数字。

package hw4;


public class s1 {

    public static void main(String[] args) {

        int input;
        int min;
        int min2;



        System.out.print("Enter a value to act as sentinel:");
        int sentinel = IO.readInt();

        System.out.println("Enter numbers");
        input = IO.readInt();

        do {

            min = input;
            min2 = input;

            if (input < min) {

                min = input;

            }



            input = IO.readInt();

        } while (input != sentinel);

        // Return the results
        System.out.println("The smallest Number is: " + min);
        System.out.println("The second smallest Number is: " + min2);
    }
}

回答by Stephen C

I'm assuming this is homework, based on the nature of the question. So ...

根据问题的性质,我假设这是作业。所以 ...

Hint: if xwasthe smallest and yis nowthe smallest, then xis nowthe second smallest.

提示:如果x最小的,y现在是最小的,那么x现在是第二小的。

Hint 2: make sure your program works when the smallest and second smallest values are equal.

提示 2:确保您的程序在最小和第二小的值相等时工作。

回答by sahhhm

When you find a new min, save the previous minimum in your min2variable and then reassign minto the current input.

当您找到新的最小值时,将先前的最小值保存在您的min2变量中,然后重新分配min给当前输入。

   if (input < min) {
        min2 = min
        min = input;

    }

回答by Cokegod

This would work if you needed to find only the smallest number, but you need to find the two smallest numbers, so the ifshould go like that:

如果您只需要找到最小的数字,这将起作用,但是您需要找到两个最小的数字,所以if应该是这样的:

if(input < min) {
    min2 = min;
    min = input;
}
else if (input < min2) {
    min2 = input;
}

And I know min & min2 need a value at start, so instead of changing their value to input every time, just do that at the fist time. So also get this part out of the do:

而且我知道 min 和 min2 在开始时需要一个值,所以不要每次都将它们的值更改为输入,而是在第一次这样做。所以也把这部分从do:

min = input;
min2 = input;