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:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* 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
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
LinkBack Thread Tools
Old 10-31-2005, 11:24 AM   #1 (permalink)
YWG
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);
}
}
YWG is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
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

Old 11-01-2005, 08:47 AM   #2 (permalink)
Registered User
 
ricer333's Avatar
 
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)
ricer333 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 11-02-2005, 08:38 AM   #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);
	   }
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.
DJ_Dance is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 07:54 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85