我的目标是输入三个数字并让程序吐出中间的数字。这适用于做 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
错误的方法?