Ok so I have written a program that acts like a magic 8 ball. Here is what i want it to do. I want to ask it 3 questions ending ain a question mark, and then a forth without a question mark. The program will then prompt the user and tell him that it wasn't a question. Then enter a Q or q at that screen to automatically quit. But my program will not do this, can someone edit my code and help me out?
Thanks
import javax.swing.JOptionPane;
public class magic8ball {
public static void main (String[] args) {
//Declarations//
String question = " ";
//Variables//
int outcome = 0;
//Greeting//
JOptionPane.showMessageDialog(null,"Welcome to the Magic 8 Ball!");
System.out.println("Welcome to the Magic 8 Ball!");
//While and Input//
while(!question.equalsIgnoreCase("Q") && !question.equalsIgnoreCase("q")) {
question = JOptionPane.showInputDialog("Please enter a question. Push q or Q to quit.");
//Processing and Possible Outcomes//
if(question.equals("Q") && question.equals("q")) {
JOptionPane.showMessageDialog(null,"Thank you for consulting the Magic 8 Ball. Good Bye.");
System.out.println("Thank you for consulting the Magic 8 Ball. Good bye.");
System.exit(0);
}
while(question.charAt(question.length()-1)!='?') {
System.out.println("You asked: " + question + '\n' + "That is not a question!");
JOptionPane.showMessageDialog (null, "You asked: " + question + '\n' + "That is not a question!");
question = JOptionPane.showInputDialog("Enter a question. Press q or Q to quit.");
}
outcome = (int) (Math.random()*8) + 1;
if(outcome==1) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "Signs point to yes.");
System.out.println("You asked: " + question + "\nThe Answer: " + "Signs point to yes.");
}
if(outcome==2) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "It is decidedly so.");
System.out.println("You asked: " + question + "\nThe Answer: " + "It is decidedly so.");
}
if(outcome==3) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "Outlook good.");
System.out.println("You asked: " + question + "\nThe Answer: " + " Outlook good.");
}
if(outcome==4) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "Reply hazy, try again.");
System.out.println("You asked: " + question + "\nThe Answer: " + "Reply hazy, try again.");
}
if(outcome==5) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "Cannot predict now.");
System.out.println("You asked: " + question + "\nThe Answer: " + "Cannot predict now.");
}
if(outcome==6) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "My sources say no.");
System.out.println("You asked: " + question + "\nThe Answer: " + "My sources say no.");
}
if(outcome==7) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "Very doubtful.");
System.out.println("You asked: " + question + "\nThe Answer: " + "Very doubtful.");
}
if(outcome==8) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "Don't count on it.");
System.out.println("You asked: " + question + "\nThe Answer: " + "Don't count on it.");
}}
System.exit(0);
}
}
Thanks
import javax.swing.JOptionPane;
public class magic8ball {
public static void main (String[] args) {
//Declarations//
String question = " ";
//Variables//
int outcome = 0;
//Greeting//
JOptionPane.showMessageDialog(null,"Welcome to the Magic 8 Ball!");
System.out.println("Welcome to the Magic 8 Ball!");
//While and Input//
while(!question.equalsIgnoreCase("Q") && !question.equalsIgnoreCase("q")) {
question = JOptionPane.showInputDialog("Please enter a question. Push q or Q to quit.");
//Processing and Possible Outcomes//
if(question.equals("Q") && question.equals("q")) {
JOptionPane.showMessageDialog(null,"Thank you for consulting the Magic 8 Ball. Good Bye.");
System.out.println("Thank you for consulting the Magic 8 Ball. Good bye.");
System.exit(0);
}
while(question.charAt(question.length()-1)!='?') {
System.out.println("You asked: " + question + '\n' + "That is not a question!");
JOptionPane.showMessageDialog (null, "You asked: " + question + '\n' + "That is not a question!");
question = JOptionPane.showInputDialog("Enter a question. Press q or Q to quit.");
}
outcome = (int) (Math.random()*8) + 1;
if(outcome==1) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "Signs point to yes.");
System.out.println("You asked: " + question + "\nThe Answer: " + "Signs point to yes.");
}
if(outcome==2) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "It is decidedly so.");
System.out.println("You asked: " + question + "\nThe Answer: " + "It is decidedly so.");
}
if(outcome==3) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "Outlook good.");
System.out.println("You asked: " + question + "\nThe Answer: " + " Outlook good.");
}
if(outcome==4) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "Reply hazy, try again.");
System.out.println("You asked: " + question + "\nThe Answer: " + "Reply hazy, try again.");
}
if(outcome==5) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "Cannot predict now.");
System.out.println("You asked: " + question + "\nThe Answer: " + "Cannot predict now.");
}
if(outcome==6) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "My sources say no.");
System.out.println("You asked: " + question + "\nThe Answer: " + "My sources say no.");
}
if(outcome==7) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "Very doubtful.");
System.out.println("You asked: " + question + "\nThe Answer: " + "Very doubtful.");
}
if(outcome==8) {
JOptionPane.showMessageDialog(null,"You asked: " + question + "\nThe Answer is: " + "Don't count on it.");
System.out.println("You asked: " + question + "\nThe Answer: " + "Don't count on it.");
}}
System.exit(0);
}
}