简体   繁体   English

如何在for循环中使用min(x,y)计算最小值? 取决于用户输入

[英]How to calculate the minimum value using min(x, y) in a for loop? Dependent on user input

So I have been trying to tackle this problem for a while. 所以一段时间以来,我一直在努力解决这个问题。 Many things I have attempted have failed. 我尝试过的许多事情都失败了。 Is there a way to add to what I already have? 有没有办法增加我已经拥有的?

for(int ii = 1, j = 0; j <= copySel ; ii++, j++) {    
             int check;
             int x, y;
                // Prompt as follows
                System.out.print("Enter value " + ii + ": ");
                try {
                    c = Get();
                }
                catch (InputMismatchException e) {
                    input
                    System.out.println("Invalid input!");
                    ii--;
                }check = c;   
                 x = check; // I want to try to copy this 
                 y = check - 1; // and copy this
                min(x , y) // trying to acheive this




                System.out.println(check + " " + x + " " + y);

        }

Sorry about the formatting. 抱歉,格式化。 It is my screen. 这是我的屏幕。

Basically let us say user puts in 25 for first input. I want x = 25.
Then user inputs -11.                                 I want y = -11.
Compare minimum                                       z = -11.
then                                                  x = z = -11;
User input 33.                                        y = 33
annd so on..

So i ended up with something like 所以我最终得到了类似的东西

for(int ii = 1, j = 0; j <= copySel ; ii++, j++) {    
             int check;
             int x = 0, y = 0, z;
                // Prompt as follows
                System.out.print("Enter value " + ii + ": ");
                try {
                    c = Get();
                }
                catch (InputMismatchException e) {

                    System.out.println("Invalid input!");
                    ii--;
                }check = c;
               x = check;
               z = x;   
            if (j % 2 == 1)
            {
                y = check;
                Math.min(z, y);
            }


                System.out.println(check + " " + x + " " + y + " " + z);

        }

I guess you are trying to get the minimum value from the current input and last minimum value. 我猜您正在尝试从当前输入和最后一个最小值中获取最小值。 Definitely it will be the minimum of all the numbers provided. 肯定会是所提供的所有数字中的最小值。 Then all you need is to save minimum value from the last turn and compare it with input in the next one. 然后,您需要保存的是上一轮的最小值,并将其与下一轮的输入值进行比较。

for(int ii = 1, j = 0; j <= copySel ; ii++, j++) {  //btw, what is 'j' for? attempt count?
         boolean mode; //true = minimum, false = maximum
         boolean firstNumber = true;
         int check; //user input
         int lastValue; //will be the storage
            // Prompt as follows
            System.out.print("Enter value " + ii + ": ");
            try {
                c = Get();
            }
            catch (InputMismatchException e) {
                input
                System.out.println("Invalid input!");
                ii--;
            }
             check = c;
             if (firstNumber) { //first input has nothing to compare with
                 lastValue = check;
                 firstNumber = false;
                 continue;
             }
             lastValue = mode ? min(check, lastValue) : max(check, lastValue);
             /* you should also implement max() method if you want to be able to find maximum
             *  if you don't, just write lastValue = min(check, lastValue);
             *  and remove other 'mode' dependancies.
             */


             System.out.println((mode ? "minimum is: " : "maximum is: ") + String.valueOf(lastValue));

    }

hope I have understood your problem correct. 希望我正确理解了您的问题。

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

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