![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| Welcome
to Tech Support Forum home to more then 136,000 problems solved. Issues
have included: Spyware, Malware, Virus Issues, Windows, Microsoft,
Linux, Networking, Security, Hardware, and Gaming Getting your
problem solved is as easy as: 1. Registering for a free account 2. Asking your question 3. Receiving an answer Registered members: * See fewer ads. * And much more..
|
| Want to know how to post a question? click here | Having problems with spyware and pop-ups? First Steps |
|
|||||||
| Programming A discussion forum for programs and programming used in tech-related businesses. |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Registered User
Join Date: Oct 2005
Posts: 1
OS: XP
|
Java Help
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); } } |
|
|
|
| Important Information |
|
Join the #1 Tech Support Forum Today - It's Totally Free!
TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free. Join TechSupportforum.com Today - Click Here |
|
|
#2 (permalink) |
|
Registered User
Join Date: Sep 2004
Posts: 302
OS: Vista (on Laptop) WinXP Pro SP3 (on Desktop)
|
'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?
__________________
AMD X2 5000+ Black w/ XFX GeForce 8200 MoBo 4GB OCZ DDR2 RAM 3/4 of a TB of data storage between 2 HDD's (no RAID) |
|
|
|
|
|
#3 (permalink) |
|
Registered User
Join Date: Jul 2005
Posts: 24
OS: Windows XP
|
*********** 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);
}
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.");
}
Hope that helps. |
|
|
|
![]() |
| Thread Tools | |
|
|