【问题标题】:How to calculate the minimum value using min(x, y) in a for loop? Dependent on user input如何在 for 循环中使用 min(x, y) 计算最小值?取决于用户输入
【发布时间】:2012-11-01 15:51:43
【问题描述】:

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

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

        }

【问题讨论】:

  • 你想找到最少两个数字吗?请发布您尝试过的许多事情之一。
  • 如果用户决定设置 3 或 4 来比较的话会超过两个
  • 你的这个问题没有解决你的问题吗? stackoverflow.com/questions/13356400/…
  • y 不会一直是x - 1?计算最小值似乎很愚蠢;在程序的那个时候,它总是y。另外,你想用最小值做什么?把它存放在某个地方?
  • 你想达到什么目的?不要只是粘贴一些不起作用的代码。解释您想要做什么以及什么(确切地说是错误消息、异常......)不起作用。

标签: java for-loop min


【解决方案1】:

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

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-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多