Results 1 to 4 of 4
  1. #1
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default Need help with loop!!

    Hi my tutor set me a task to complete, basically i need my program here to loop back to the optionSelected part of my code, i know how to write to it counts the vote ect, i just need to know how to loop the whole sequence so it doesn't just close when a user votes for a team....

    Java Code:
    import Barnfield.Dialogs;
    
    class VoteSystem {
    	public static void main (String args[]) {
    		int optionSelected;
    		double total;
    		
    		Vote question1 = new Vote();
    		question1.setQuestionNumber(1);
    		question1.setQuestion("Arsenal");
    		
    		Vote question2 = new Vote();
    		question2.setQuestionNumber(2);
    		question2.setQuestion("Bayern Munich");
    		
    		Vote question3 = new Vote();
    		question3.setQuestionNumber(3);
    		question3.setQuestion("Chelsea");
    		
    		Vote question4 = new Vote();
    		question4.setQuestionNumber(4);
    		question4.setQuestion("Fenebarche");
    		
    		optionSelected = Dialogs.integerInput("Select: \n 1: View \n 2: Vote \n 3:Exit");
    		
    		if (optionSelected > 3){
    			Dialogs.errorMessage("You entered a wrong number");
    		}
    		if (optionSelected == 1){
    			Dialogs.outputMessage("The current score is ");
    			}
    		if (optionSelected == 2){
    			optionSelected = Dialogs.integerInput("Enter a number for the following answers: \n 1: Arsenal \n 2: Bayern Munich \n 3: Chelsea \n 4: Fenebarche");
    		if (optionSelected == 1)
    			Dialogs.outputMessage("You Voted For :" + " " + question1.getQuestion());
    		if (optionSelected == 2)
    			Dialogs.outputMessage("You Voted For :" + " " + question2.getQuestion());
    		if (optionSelected == 3)
    			Dialogs.outputMessage("You Voted For :" + " " + question3.getQuestion());
    			
    		if (optionSelected == 4)
    			Dialogs.outputMessage("You Voted For :" + " " + question4.getQuestion());
    		}
    		if (optionSelected > 4){
    			Dialogs.errorMessage("You entered a wrong number");
    		}
    		if (optionSelected == 3){
    			Dialogs.outputMessage("Program Closing");
    		}
    
    		System.exit(0);
    	}
    
    		
    		}
    Many Thanks Chris

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do you understand the three types of loops? Do ... While, while, and for.

    Do while ensures that the loop body is performed at least once
    Java Code:
    do{
      Something fun
    }while(condition is true);
    While is good for looping x times, where x is unknown
    Java Code:
    while(condition is true){
      Do something less fun
    }
    A for loop is good for doing something x times, where x is known
    Java Code:
    for(int i = 0; condition is true; i++){
      Do something more fun
    }
    The oracle tutorials have more detailed explanations of all three. Which loop you need is upmto what you want, all three loops can function the same way.
    Last edited by sunde887; 04-24-2011 at 03:17 PM.

  3. #3
    chrisLU5 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    I dont understand them fully, i just need a starting point, i don't understand that when option 2 is selected and the user votes for a team how to return back to the main menu to select option 1 to view the score?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  2. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  3. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 PM
  4. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  5. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 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
  •