Skip to content
Advertisement

How do I return to the beginning of a while loop?

I’ve only been taking coding classes for a month, so I’m really sorry if I’m using some wrong/wacky constructions.

I’m trying to make an addition game using two random numbers; r1 determines the number of terms in the addition problem, while r2 assigns a value to each of them. The scanner checks the user’s input and based on that the game either gives points or counts failed attempts.

The problem is that the first iteration runs fine, but it never displays a new problem, even though the condition of the main while loop (3 wrong answers) hasn’t been met.

Could you please give me some pointers? Thanks!

JavaScript

Advertisement

Answer

I thought you might want to see a simpler implementation of this game, as I noticed you are struggling to figure out what variables you need, what data structures will help etc.!

Hope this helps you understand a bit better how to structure your programs in Java:

JavaScript

I would recommend splitting up the loops into separate methods after you understand my changes… that will make it much easier for you to “focus” on a single part of the game instead of having to keep everything in your head while you read the code… I moved most fields to local variables because it’s ALWAYS preferrable to have only a small number of things at scope (the variables you can “see”) at any given time – again, to help the programmer understand what values matter and can/should be used.

Good luck and hope you have a great career as a programmer.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement