解决方案1
您可以重复操作2次这样的:
public static void main(String[] args) {
int i = 0, n = 2;//repeat n time
while (i < n) {
// TODO code application logic here
int num1;
num1 = Integer.parseInt(JOptionPane.showInputDialog("Please enter a value"));
if (num1 < 50 && num1 > 100) {
System.out.println("value is correct");
} else {
System.out.println("value is incorrect");
}
i++;
}
}
解决方案2
您可以使用数组来存储您的值并稍后检查它们,例如:
public static void main(String[] args) {
int i = 0, n = 2;
// TODO code application logic here
int num1[] = new int[n];
while (i < n) {
num1[i] = Integer.parseInt(JOptionPane.showInputDialog("Please value " + (i+1)));
i++;
}
if (num1[0] < 50 && num1[1] > 100) {
System.out.println("value is correct");
} else {
System.out.println("value is incorrect");
}
}
这会问你的N值,你的情况会要求你输入2倍的值,因此将存储阵列中,那么你可以检查数组的这个值。
编辑
你必须使用一个分隔符,你可以用这个分离例如您输入应该是这样划分:
6999,888
--1---2
所以当你与,
String[] spl = res.split(",");
分裂你会得到一串像[6999,888]
的字符串,那么你可以使用这两个值来使你的条件:
int v1 = Integer.parseInt(spl[0]);//convert String to int
int v2 = Integer.parseInt(spl[1]);//convert String to int
所以,你的程序应该是这样的:
public static void main(String[] args) {
String res = JOptionPane.showInputDialog("Please enter a value separated with , :");
String[] spl = res.split(",");
System.out.println(Arrays.toString(spl));
//you have to make some check to avoid any problem
int v1 = Integer.parseInt(spl[0]);
int v2 = Integer.parseInt(spl[1]);
if (v1 < 50 && v2 > 100) {
System.out.println("value is correct");
} else {
System.out.println("value is incorrect");
}
}
EDIT2
您可以显示你的结果的JOptionPane这样的:
if (v1 < 50 && v2 > 100) {
JOptionPane.showMessageDialog(null, "value is correct");
} else {
JOptionPane.showMessageDialog(null, "value is incorrect");
}
EDIT3
要获得最大你必须像这样检查它:
if (v1 > v2) {
JOptionPane.showMessageDialog(null, "larger value is : " + v1);
} else {
JOptionPane.showMessageDialog(null, "larger value is : " + v2);
}
或者在同一行,你可以使用:
JOptionPane.showMessageDialog(null, "larger value is : " + (v1 > v2 ? v1 : v2));
感谢您的帮助,但是这并没有解决我的问题,不幸的是我相信这是由于我的错误,我需要输入对话框接受值为(55 74),然后确定它们是否落在指定范围内,并最终在消息对话框中显示两个数字中较大的一个,感谢您的帮助至此 –
您是否尝试了第二种解决方案@ SaienLakshuman? –
是的,我尝试了两种解决方案 –