Reseting guess count
I need help on how to reset the guess count (numTries) back to 0 every time i start another game round.
import java.util.Scanner;
import java.util.Random;
import static java.lang.System.*;
public class lab14c
{
public void run()
{
Scanner keyboard = new Scanner(in);
Random rand = new Random();
int num;
int guess;
int percent;
int numTries=0;
int randomNum;
String a = "";
do{
out.print("\nGuess Game - how many numbers? ");
num = keyboard.nextInt();
randomNum = rand.nextInt(num-1)+1;
do{
out.print("Enter a number between 1 and " +num+ " ");
guess = keyboard.nextInt();
if(guess<1 || guess>num)
{
out.println("Number out of range!");
}
else if(guess!=randomNum+1)
{
numTries++;
}
out.println("count = " +numTries);
}while(guess!=randomNum);
if(randomNum==guess)
{
percent = ((numTries-1)*100)/(num);
out.println("\nIt took " +numTries+ " guesses to guess " +randomNum+ ".");
out.println("You guessed wrong " +percent+ " percent of the time.");
}
out.print("\nDo you want to play again? ");
a = keyboard.next();
if(a.equals("n"))
break;
}while(num>0);
}
public static void main(String args[])
{
lab14c test = new lab14c();
test.run();
}
}
The guess count (numTries) keeps carrying over to the next game so it screws up the percentage. Please any advice would be appreciated.
Message was edited by:
Odainknight

