Sign in to follow this  
mrZiffix

[Java] help with writing a loop?

Recommended Posts

mrZiffix    100
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.

[CODE]
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");

}
}

}
}

[/CODE]

Share this post


Link to post
Share on other sites
SimonForsman    7642
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 this post


Link to post
Share on other sites
rip-off    10976
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:
[list]
[*] 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.
[code]

if (answer < number) {
// ...
} else if (answer > number) {
// ...
} else { // answer == number
// ...
}
[/code]
If you were developing a large project, it even be a good idea to put an [i]assertion[/i] in the final clause to ensure that even as the program is being updated and maintained that the expected condition still holds.
[/list]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this