Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-18-2007, 08:23 PM
Member
 
Join Date: Jul 2007
Posts: 35
silvia is on a distinguished road
Help me: loops in java
I have a very simple exercise. I am trying to call several methods from the main. I can call the method and the java input works but I can't get it to go back to the main after a valid selection has been made.
Can someone steer me in the right direction please?

Code:
class sampledata { public static final void main (String[] argv) { System.out.println("I am going to ask you some questions. Please answer using capital letters for any A,B,C choices. Thankyou."); System.out.println("Do you live in the "); options1(); question2(); }//end main static void options1() { System.out.println("(A) Northern Hemisphere"); System.out.println("(B) Southern Hemisphere"); System.out.println("(C) neither of the above"); int count = 2; do { KeyboardReader kb = new KeyboardReader(); char choose; choose = kb.getChar(); switch (choose) { case 'A': System.out.println("Ahah, so it is Spring right now for you."); break; case 'B': System.out.println("Like me you are approaching Winter. I hope it is mild for you."); break; case 'C': System.out.println("Hmmm, surely you know what hemisphere you live in. Try another selection."); break; default: System.out.println("That was not a valid selection"); } }while (count>1); } static void question2() { System.out.println("Question 2 not yet prepared. Come back soon."); } }
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-18-2007, 10:58 PM
Member
 
Join Date: Jul 2007
Posts: 4
gradon is on a distinguished road
use a while?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-19-2007, 05:20 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ya, use the while lop until your requirement is satisfying.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-19-2007, 07:47 PM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
Code:
int count = 2; do { KeyboardReader kb = new KeyboardReader(); char choose; choose = kb.getChar(); switch (choose) { case 'A': System.out.println("Ahah, so it is Spring right now for you."); break; case 'B': System.out.println("Like me you are approaching Winter. I hope it is mild for you."); break; case 'C': System.out.println("Hmmm, surely you know what hemisphere you live in. Try another selection."); break; default: System.out.println("That was not a valid selection"); } }while (count>1);
i dunno why did u use da count variable, but from what i c it, of course u cant go back to the main, da loop will go on and on as long as the count are still bigger than 1
Code:
}while (count>1);
u didnt put any changes to da variable count in ur program after da valid selection A or B or C, maybe after da System.out.print() statement, u should put some means to change da count value from 2 to 1 or less, so that it can escape da loop...

jst correct me if i got ur point wrong k
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need help with Slope (Loops) Zebra New To Java 9 04-18-2008 04:39 AM
Question about loops BHCluster New To Java 4 04-16-2008 06:40 PM
[SOLVED] Need help with Loops...please! Zebra New To Java 5 04-10-2008 02:44 PM
Loops (while do etc) manupr New To Java 1 01-15-2008 04:59 AM
Nested loops? gabriel New To Java 4 08-06-2007 05:51 PM


All times are GMT +3. The time now is 06:50 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org