Can anyone help me with my if and else statements? It's only saying "Exit Goodbye" whenever I input something, which should only happen when I enter -0. My teacher is gone for the week, so I don't have anyone to ask for help.
package game;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
import javax.swing.JOptionPane;
public class GameFrame {
/**
* @param args
*/
public static void main(String[] args) {
// num1 - Variable to store the first value
// num2 - Variable to store the second value
// answer - Variable to accept user input
int num1, num2, answer=0;
/*@reader - The reader which accepts user input*/
BufferedReader reader = new BufferedReader (new InputStreamReader(System.in));
/*@quit - Variable used to exit the program*/
boolean quit = false;
/*@generator - The Random number generator*/
Random generator = new Random();
while (quit == false)
{
//Generate First Random Number between 1-100
num1 = generator.nextInt(100);
//Generate First Random Number between 1-100
num2 = generator.nextInt(100);
//Displays the math equation
String input = JOptionPane.showInputDialog(null,num1+ "+" + num2 + " = ");
//Accepts the user's input and converts it to int value
int number = Integer.parseInt(input);
//Lets assume if user enters -99, it means they want to exit the program
if (answer == -0)
{
JOptionPane.showMessageDialog(null, "Exit Program: Good Bye!
");
quit = true;
}else if (answer == (num1+num2))
JOptionPane.showMessageDialog(null,"Correct Answer!
");
else{
JOptionPane.showMessageDialog(null,"Incorrect Answer
");
}
}
}
}
See Question&Answers more detail:os