Reputation: 53
I am writing a number-guessing game for my Computer Science 1100 class, one part of which is to print the number of attempts the player takes at guessing the target number. I've defined a variable tries
to track that; the program increments it by one every time the player makes a guess.
The game restarts after the player guesses the number correctly, and at that point I want to reset the tries
counter. I can't figure out how to do that, however, because the program increments tries
each time the number is guessed. How can I do it?
import java.util.Scanner;
import java.util.Random;
public class Q2 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
Random r = new Random();
System.out.println("Welcome to the Number Guessing Game");
int x=0;//defining x for later
int tries = 1;//defining tries for later
while (x!=-1) {
int y = r.nextInt(101);//defining random number 0-100
System.out.print("Guess a number between 0 and 100 or enter -1 to quit: ");
x=kbd.nextInt();//redefining x
x=kbd.nextInt();//redefining x
for (int i=1;x!=-1&&x!=y;i=1) {//for loop
if (x<-1||x>100) {//illegal condition
System.out.print("Out of bounds. Try again: ");
}
else if (x>y) {//input greater than random condition
System.out.print("The number is lower. Try again: ");
}
else if (y>x) {//random greater than input condition
System.out.print("The number is higher. Try again: ");
}
x = kbd.nextInt();//redefining x
tries+=i;//defining pattern for tries
}
if (x==y) {//input=random condition
System.out.println("Congratulations! You guessed the number in " + tries + " tries");
}
}
if (x==-1) {//quit condition
System.out.print("Thank you for playing the game!!");
}
}
}
Upvotes: 1
Views: 17875
Reputation: 1
One possible solution would be to encapsulate your guessing game into it's own method, which will re-initialize all variables when called, and create an if
condition inside of your loop if the guess is correct.
private static void guessingGame() {
Scanner scanner = new Scanner(System.in);
double randomNum = Math.random();
double tries = 0, guess = 0;
do while (!(guess == randomNum) {
System.in("What is your guess? ");
guess = scanner.nextDouble();
tries++;
if (guess == randomNum) {
System.out.println("Guessed correctly in " + tries + " tries.");
tries = 0;
}
}
}
public static void main() {
guessingGame();
}
Upvotes: 0
Reputation: 2264
Variables are only available in the scope they are defined in. For example
while (something) { // all code inside the loop is in an inner scope
int variable = 42;
// variable is accessible here
}
// variable is not accessible here
This means, every time the while-loop performs one iteration, variable is newly created. It is a good practice to only define variables in the scope they actually have a meaning (in this case in the while-loop).
Another way would be to reset the variable each time it is necessary. This would result in such a design:
int variable; // variable is defined outside the inner scope
while (something) {
variable = 42;
// some code that changes variable's value
}
Upvotes: 8