random - nested loop, how to end loop and how to get loop to continue to cycle? java, eclipse -
My program will not do that I need it and I got stuck on this issue for six hours. It indicates to the user how many times they want to toss a coin, according to the program, how many heads and tails were thrown, and with the total head and tail as well as the percent which had been rolled into the head, my problem is That it goes through for the first time once the loop ends it just stops and the user does not indicate any other number to choose or leave the program For the sector and press 0 to return to the main menu, I can how to fix it myself? Apart from this, how can I get the option of returning to the second loop? I have tried the break method and it does not work for me. I do not know if I'm actually lost. How can I make it, this loop continues to toss the coin and still ask users how often they want to toss. While at the same time pressing Just 0 and allowing it to exit the loop to return to the main menu? I did not post that code here, but it is a little loop with various sub options / games.
if (the second scanner hasNext ("t | T")) {c = anotherScanner.next (); UsersSelection = True; Println (""); System.out.println ("cone toss simulators"); Println (""); System.out.println ("0 to leave. How many toss?"); Random rand = new random (); Float head count = 0; Float tail number = 0; Scanner Scanman = New Scanner (System.in); Int numero = scanMan.nextInt (); Boolean headrestel; {If (numero == 0) {break;} headsOrTails = rand.nextBoolean (); for {int j = 0; j & lt; number; j ++} If (headlines == true) {headsCount ++; } Other {tail number ++; System.out.println (headCount + "heads and" + tail number + "tail means" ++ "(headcount / (circount + tail number) * 100 +"% head ")); }}}}
You have boolean flag
variable external Use break
s / continue
s to nested loops before loop, and to achieve your desired control flow.
Sample code:
Boolean flag = false; For (int i = 0; i & lt; N; i ++) {for (int j = 0; j & lt; n; j ++) {if (j & gt; i) {flag = true; break; }} If (flag) {break; }}
... or if you need to use continue
instead of changing the internal loop termination condition as you need. Alternatively, if you are a fan of goto
, then immediately after the reply See related answers, exit the loop and jump to a particular label.
Sample code (from that SO page):
for loops: (int i = 0; i
Comments
Post a Comment