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



,所以我一直试图解决这个问题。我尝试过的许多事情都失败了。有没有办法添加我已经拥有的东西?

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

对格式感到抱歉。这是我的屏幕。

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..

所以我最终得到了

之类的东西
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);
        }

我想您正在尝试从当前输入和最后一个最小值中获取最小值。绝对是提供的所有数字的最低限度。然后,您需要的只是从最后一回合节省最小值,并将其与下一个输入进行比较。

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

希望我知道您的问题是否正确。

最新更新