Java How can I count the number of times a loop runs in Java?

  • Thread starter Thread starter schapman22
  • Start date Start date
  • Tags Tags
    Count Java Loops
AI Thread Summary
To create a "Guessing Game" program that counts the number of attempts taken to guess a number between 1-1000, a counter variable is essential. This variable should be initialized to zero and incremented by one each time the guessing loop runs. At the end of the game, the final value of the counter will indicate how many guesses were made. This approach effectively tracks the number of iterations in the loop, providing the necessary feedback when the correct number is guessed.
schapman22
Messages
74
Reaction score
0
For my class I need to make a "Guessing Game" Program. Basically you just pick a number between 1-1000. It tells you if you are too high, too low, or correct. If your too high or low you guess again until you get it right. When you get it right it is supposed to tell you how many tries it took you. I have it running properly but I don't know how to count how many times the loop ran, which is the number I would need for the response when you guess the correct number. Can anyone tell me how to count the number of times a loop runs?
 
Technology news on Phys.org
Use a counter variable. You just add 1 each time the thing you want to count happens (i.e. add 1 each time through the loop) and check its value at the end.
 
Thank you.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top