0

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

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

        }
4

1 回答 1

0

我猜您正在尝试从当前输入和最后一个最小值中获取最小值。绝对是提供的所有数字中的最小值。然后,您只需要保存上一轮的最小值并将其与下一轮的输入进行比较。

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

    }

希望我已经正确理解了您的问题。

于 2012-11-13T07:59:05.253 回答