Results 1 to 4 of 4
Thread: Need help with loop!!
- 04-24-2011, 03:07 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
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....
Many Thanks ChrisJava 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); } }
- 04-24-2011, 03:14 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
While is good for looping x times, where x is unknownJava Code:do{ Something fun }while(condition is true);
A for loop is good for doing something x times, where x is knownJava Code:while(condition is true){ Do something less 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.Java Code:for(int i = 0; condition is true; i++){ Do something more fun }Last edited by sunde887; 04-24-2011 at 03:17 PM.
- 04-24-2011, 03:26 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
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?
- 04-24-2011, 07:02 PM #4
The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)I dont understand them fully, i just need a starting point
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
db
Similar Threads
-
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM -
Convert do while loop to for loop
By sandeeptheviper in forum New To JavaReplies: 3Last Post: 01-03-2011, 12:37 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks