解決方案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? –
是的,我嘗試了兩種解決方案 –