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 に答える