Results 1 to 4 of 4
  1. #1
    silvia is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default 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?

    Java 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

  2. #2
    gradon is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default

    use a while?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Ya, use the while lop until your requirement is satisfying.

  4. #4
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Java 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
    Java 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 :p

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •