Hi all
I am currently learning Java (and Fortran!) on my own. I have written the following code but for some reason it does not work as it should
import javax.swing.JOptionPane;
class evenOdd {
public static void main(String[] args) {
String number, ans;
int num;
do {
number = JOptionPane.showInputDialog(null, "Enter a number (int)", "Even or Odd?", JOptionPane.QUESTION_MESSAGE);
num = Integer.parseInt(number);
if (num%2 == 1)
JOptionPane.showMessageDialog(null, "The number is Odd", "Even or Odd!", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "The number is even", "Even or Odd!", JOptionPane.INFORMATION_MESSAGE);
ans = JOptionPane.showInputDialog(null, "Do you want to try again? (yes/no)", "Try Again?", JOptionPane.QUESTION_MESSAGE);
} while (ans == "yes");
}
}
I have changed the code a little so the user will answer the last question via numbers (1/0) and it worked as it should. Here is the changed version of the code:
import javax.swing.JOptionPane;
class evenOdd {
public static void main(String[] args) {
String number, answer;
int num, ans;
do {
number = JOptionPane.showInputDialog(null, "Enter a number (int)", "Even or Odd?", JOptionPane.QUESTION_MESSAGE);
num = Integer.parseInt(number);
if (num%2 == 1)
JOptionPane.showMessageDialog(null, "The number is Odd", "Even or Odd!", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "The number is even", "Even or Odd!", JOptionPane.INFORMATION_MESSAGE);
answer = JOptionPane.showInputDialog(null, "Do you want to try again? (1/0)", "Try Again?", JOptionPane.QUESTION_MESSAGE);
ans = Integer.parseInt(answer);
} while (ans == 1);
}
}
I will appreciate any help to understand this issue.
~faizlo
PS.
This is my first message here, so I hope I did not break any rule.