简体   繁体   English

如何找到Java中输入的最小序列数

[英]How to find the minimum number of sequence input in Java

I am devoloping an application to find the minimum of all the numbers entered .It accepts the numbers from the dialog box and when the user enters 0 it displays the minimum of all the numbers.But i dont need the 0 but the minimum of the numbers that preceeded it. 我正在开发一个应用程序以查找输入的所有数字中的最小值。它接受对话框中的数字,当用户输入0时,它将显示所有数字中的最小值。但是我不需要0但需要数字中的最小值在此之前。

My code is as Follows: 我的代码如下:

    try {
        int a, c = 0, d = 0;

        do {
            a = Integer.parseInt(jOptionPane1.showInputDialog(null, "please enter the number"));

            c = Math.min(a, d);
            if (a != 0) //since a=0 will be excecuted one time
            {
                d = c;
            }
        } while (a != 0);

        lb2.setText("Minimum of the numbers is " + d);



    } catch (Exception e) {
        jOptionPane1.showMessageDialog(this, "Some thing went wrong");
    }

I know that it gives me 0 because the minimim of the numbers entered is zero and if i enter a number less than 0 (ie a negative number)it gives me the correct answer .I think the problem is also due to the initialisation that c=0. 我知道它给了我0,因为输入的数字的最小值为零,如果我输入的数字小于0(即负数),它给了我正确的答案。我认为问题还在于初始化c = 0。

Now i need a method to find the minimum without using any arrays and it should be simple and easy.(most helpful if you use Math.min itself) 现在我需要一种无需使用任何数组即可找到最小值的方法,它应该简单易行。(如果您使用Math.min本身最有用)

Any help Appreciated. 任何帮助表示赞赏。

只需将您的初始化更改为将d设置为Integer.MAX_VALUE

I have an advice for your code, that you should make every variable names make sense. 我对您的代码有建议,建议您使每个变量名都有意义。 Maybe your code is small, but it will affect your habit, and when you work in large project, your habit will affect you so much :) 也许您的代码很小,但它会影响您的习惯,而当您在大型项目中工作时,您的习惯将对您造成很大的影响:)

You should change initialize part of d = 0 to d = Integer.MAX_VALUE . 您应该将d = 0初始化部分更改为d = Integer.MAX_VALUE Here is a new code : 这是一个新代码:

try {
        int inputNumber = 0, min= Integer.MAX_VALUE;
        // int c : you don't need this variable

        do {
            inputNumber = Integer.parseInt(jOptionPane1.showInputDialog(null, "please enter the number"));

            if (inputNumber != 0) //since a=0 will be excecuted one time
            {
                min= inputNumber;
            }
        } while (inputNumber != 0);

        lb2.setText("Minimum of the numbers is " + min);



    } catch (Exception e) {
        jOptionPane1.showMessageDialog(this, "Some thing went wrong");
    }

This new code is make more sense ? 这样的新代码更有意义吗?

And, Why you Must change initialize to min= Integer.MAX_VALUE ? 而且,为什么必须将初始化更改为min= Integer.MAX_VALUE For example, you initialize like this : min = 10; 例如,您这样初始化: min = 10; . And at first time when someone type 15, you program will see : Oh, 15>10, so this is not the min value. 第一次有人输入15时,您会看到:哦,15> 10,所以这不是最小值。 But in fact, 15 is the first value of input and it should be the min value. 但实际上,15是输入的第一个值,应该是最小值。 Your program will be wrong until someone type a number less than 10. 除非有人输入小于10的数字,否则您的程序将是错误的。

Compare to your code, because you initialize d=0 . 与您的代码进行比较,因为您初始化了d=0 when you type d=1 , ops, 1>0, this is not min value (like above example). 当您输入d=1 ,ops,1> 0,这不是最小值(如上面的示例)。 And everything will true only when you type some numbers < 0. 仅当您键入一些小于0的数字时,所有内容才会成立。

Al the problem above happen, because although user types any number, the min is the initialize number. 所有上述问题都会发生,因为尽管用户键入任何数字,但最小值是初始化数字。 (the number that user doesn't type). (用户未输入的数字)。 And that why you set your min value to something REALLY REALLY big. 这就是为什么您将最小值设置为非常大的原因。 So, the FIRST TIME you type some numbers, It's ALREADY the SMALLEST . 所以,当您第一次输入一些数字,它已经 最小

Because machine not like us, doesn't have "infinity", so we must set it the biggest value possible. 因为机器不像我们一样没有“无限”,所以我们必须将其设置为最大可能值。 And because d is an int, so the biggest value of int is Integer.MAX_VALUE . 并且由于d是一个int,所以int的最大值是Integer.MAX_VALUE In fact, you can type number for d. 实际上,您可以为d键入数字。 I don't remember exactly, but the biggest value for integer in range 32000 (2^31). 我记不清了,但是整数的最大值在32000(2 ^ 31)范围内。

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

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