Tech Support banner

Status
Not open for further replies.
1 - 3 of 3 Posts

·
Registered
Joined
·
1 Posts
Discussion Starter #1
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);
}
}
 

·
Registered
Joined
·
310 Posts
'you're program won't do this' will you please be a bit more specific? is it compiling? what is it doing, what isn't it doing?
 

·
Registered
Joined
·
26 Posts
*********** VERY IMPORTANT **************
Indent your code. If your code isn't indented, no one will read it and therefore no one will post a reply. (Don't worry we all do at some stage in our life)

There are two things wrong with the code.

First take a look at:

Code:
           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);
	   }
That logic in the if statement doesn't seem right to me. Think about it, how can question equal 'Q' AND 'q' at the same. What you are really trying to do is
execute the body of the if statement if either occur.

The other thing

Code:
            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.");
            }
Needs to be fixed. See that if the person enters a non-question they will enter this loop. Now this loop will keep executing until the person asks a question. What if the person wants to quit without asking another question (or b/c they don't understand what's going on and just want to quit). In order to remedy the problem, (note that this is one way, but not necessarily the only way; there is never just one way in programming!) change the while loop into an 'if' statement with the same condition. Take out the last line in the body of that while; therefore you shouldn't be asking the user to enter a question again in that loop. Instead of that line try replacing it with a 'continue' statement. continue will cause the outer while loop to start from the begining; including checking the the conditions.

Hope that helps.
 
1 - 3 of 3 Posts
Status
Not open for further replies.
Top