0

我的目标是输入三个数字并让程序吐出中间的数字。这适用于做 a System.out.println,但是当我必须使用JOptionPane它时,它开始变得混乱。

这是我得到的代码:

public class ReturnMiddle {

public static void main(String[] args) {
    int num1 = 0;
    int num2 = 0;
    int num3 = 0;
    int smallest = returnMiddle(num1, num2, num3);

    String inputValue = JOptionPane.showInputDialog("What is your first number?");
    num1 = (Integer.parseInt(inputValue));

    inputValue = JOptionPane.showInputDialog("What is your second number?");
    num2 = (Integer.parseInt(inputValue));

    inputValue = JOptionPane.showInputDialog("What is your third number?");
    num3 = (Integer.parseInt(inputValue));

    JOptionPane.showMessageDialog(null, smallest);

}

    public static int returnMiddle(int num1, int num2, int num3) {

        //Returns the smaller of num1 and num2.
        int middle12 = Math.min(num1, num2);

        //Returns the smaller of num2 and num3.
        int middle23 = Math.min(num2, num3);

        //Returns the smaller of num1 and num3.
        int middle13 = Math.min(num1, num3);

        //Returns the larger of middle12 and middle13.
        int middleNumber1 = Math.max(middle12, middle13);

        //Returns the larger of middleNumber1 and middle23, which
        //ends up being the middle of the original 3 numbers.
        int middleNumber2 = Math.max(middleNumber1, middle23);

        //Makes the method "returnMiddle" return middleNumber2.
        return middleNumber2;

    }

}

当我取消输入三个数字时,我每次都得到 0。这是在两种不同方法中使用num1,num2和的问题吗?num3我是否调用了returnMiddle错误的方法?

4

3 回答 3

2

您必须在正确的地方使用该方法。目前,您首先计算结果并获得输入 SECOND。你必须反过来做。

// dont calculate it here

String inputValue = JOptionPane.showInputDialog("What is your first number?");
num1 = (Integer.parseInt(inputValue));

inputValue = JOptionPane.showInputDialog("What is your second number?");
num2 = (Integer.parseInt(inputValue));

inputValue = JOptionPane.showInputDialog("What is your third number?");
num3 = (Integer.parseInt(inputValue));

// you have to calculate the middle one here.
int middle = returnMiddle(num1, num2, num3);

JOptionPane.showMessageDialog(null, middle);
于 2013-09-27T18:39:08.690 回答
0

查看您的代码

int 最小 = returnMiddle(num1, num2, num3);

在这里,您实际上是在告诉编译器您希望 var 'smallest' 等于 returnMiddle 函数的结果。由于用户输入是在调用该方法之后,因此您不会在该调用中将这些值传递给该函数,而是实际上是初始化的 0:

int 最小 = returnMiddle(0,0,0);

获得用户输入后,您将需要调用该方法。

于 2013-09-27T18:51:35.093 回答
0

您正在调用 returnMiddle 错误。它应该在您阅读并分配输入后调用。

public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
int num3 = 0;
int smallest = Integer.MAX_VALUE;

String inputValue = JOptionPane.showInputDialog("What is your first number?");
num1 = (Integer.parseInt(inputValue));

inputValue = JOptionPane.showInputDialog("What is your second number?");
num2 = (Integer.parseInt(inputValue));

inputValue = JOptionPane.showInputDialog("What is your third number?");
num3 = (Integer.parseInt(inputValue));

smallest = returnMiddle(num1, num2, num3);

JOptionPane.showMessageDialog(null, smallest);

}

于 2013-09-27T18:38:38.380 回答