Reputation: 11
package javaapplication4;
import javax.swing.*;
public class JavaApplication4 {
public static void main(String[] args) {
// 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");
}
}
Upvotes: 1
Views: 293
Reputation: 60045
Solution 1
You can repeat the operation 2 times like this :
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++;
}
}
Solution 2
You can use an array to store your values and check them later for example :
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");
}
}
This will ask you for the n value, in your case will ask you to enter 2 values so it will stored in your array, then you can check this values of array.
EDIT
You have to use a separator and you can split with this separator for example your input should look like this :
6999,888
--1---2
so when you split with ,
String[] spl = res.split(",");
you will get an array of String like [6999,888]
, then you can use this two value to make your condition :
int v1 = Integer.parseInt(spl[0]);//convert String to int
int v2 = Integer.parseInt(spl[1]);//convert String to int
So your program should look like this :
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
You can show your result in JOptionPane like this :
if (v1 < 50 && v2 > 100) {
JOptionPane.showMessageDialog(null, "value is correct");
} else {
JOptionPane.showMessageDialog(null, "value is incorrect");
}
EDIT3
To get the max you have to check it like this :
if (v1 > v2) {
JOptionPane.showMessageDialog(null, "larger value is : " + v1);
} else {
JOptionPane.showMessageDialog(null, "larger value is : " + v2);
}
Or in one line you can use :
JOptionPane.showMessageDialog(null, "larger value is : " + (v1 > v2 ? v1 : v2));
Upvotes: 2