Public Group

# [Java] help with writing a loop?

This topic is 2493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

im not exactly sure on how to put this in but im trying to make a game on the command line that lets you guess on a random number and i cant seem to make it work since they only get to tries and then it stops. I dont know how to explain it any better but maybe if you look at the code you can see. p.s i have only been teaching myself for a week so coding might be horrible.

 import java.util.Random; import java.util.Scanner; public class One { public static void main (String args[]){ Random rand = new Random(); Scanner guess = new Scanner(System.in); int number; int answer; for (int counter = 1; counter <= 1; counter++){ number = 1+rand.nextInt(10); System.out.println("Take a guess on what the number is? "); answer = guess.nextInt(); if (answer < number){ System.out.println("Too low. Try Again"); guess.nextInt(); } else if (answer > number){ System.out.println("Too high. Try Again"); guess.nextInt(); }else if (answer == number){ System.out.println("you are right"); } } } } 

##### Share on other sites
for (int counter = 1; counter <= 1; counter++){

what this does is:
1) set counter = 1
2) Check if counter is lower than or equal to 1, if not jump to 6)
3) Run the content of the loop
4) increase counter by 1
5) Go back to 2
6) The end.

Thus your loop only runs once because you tell it to only run once.

##### Share on other sites
Generally, for loops are used when there is a fixed number of iterations, known in advance. Loops that are dependent on external events are typically written as a "while" loop. This is a stylistic choice, as any while loop can be written as a for loop, and vice versa (the for loop is syntactic sugar).

The real question is "what is the condition that must be met for the loop to end". The condition is that the player has guessed the correct number. You could use a boolean variable, initialised to false, to track if the player has succeeded in this goal. When you detect that this has happened, you set the boolean flag to true.

When you have fixed the loop, here are three other things to consider in your pgoram:

• When looping works, think about what it means to have the computer's secret generated inside the loop.
• The calls to "guess.nextInt()", without prompting the user or storing the result, are probably better avoided.
• If an integer A is not less than X, and not greater than X, then it must be equal to X. In other words, the final condition in your if/else ladder is unnecessary.
You can still use a comment to indicate the intent of the program.
 if (answer < number) { // ... } else if (answer > number) { // ... } else { // answer == number // ... } 
If you were developing a large project, it even be a good idea to put an assertion in the final clause to ensure that even as the program is being updated and maintained that the expected condition still holds.

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633742
• Total Posts
3013625
×