Cody Harrison Cody Harrison -4 years ago 108
Java Question

Track and print amount of times the while loop was used

I am writing a little mini game on jgrasp. The game is prompting a user to guess a number between 1 and 50. Then the while loop checks the "if" statement and lets them know if they guessed "to high" or "to low" and prompts them to guess again. I cannot figure out how to keep track of the amount of guesses a user takes. Any tips?? I am a beginner programmer, thanks for your help everyone.

import java.util.Scanner;

public class Harrison6c {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Welcome to the land of Hyrule!");
System.out.println("The land here is extremley dangerous");
System.out.println("Looks like you could use a sword");
System.out.println();
System.out.println("I'll tell you what lets play a game");
System.out.println("I have a number between 1 and 50");
System.out.println("Can you guess what it is?");
System.out.println("If you guess the number correctly");
System.out.println("I will give you something pretty cool!");
System.out.println();

int rand = (int)(Math.random() * (51 - 1)) + 1;

int guessInt;
guessInt = 0;

while (guessInt != rand) {
System.out.print("Enter your guess: ");
guessInt = input.nextInt();
guessInt++;

if (guessInt > rand) {
System.out.println("Guess lower");
System.out.println();
}
if (guessInt < rand) {
System.out.println("Guess higher");
System.out.println();
}
}

System.out.println("You got It!");
System.out.println("As promised here is your reward");
System.out.println("You've recieved the Kokiri Sword!");
System.out.println("Remember this land is very dangerous");
System.out.println("That sword you now yield shall protect you!");

System.out.println("You took" + guessInt++ + "guessess");
}
}


heres my output

----jGRASP exec: java Harrison6c
Welcome to the land of Hyrule!
The land here is extremley dangerous
Looks like you could use a sword

I'll tell you what lets play a game
I have a number between 1 and 50
Can you guess what it is?
If you guess the number correctly
I will give you something pretty cool!

Enter your guess: 50
Guess lower

Enter your guess: 30
Guess lower

Enter your guess: 20
Guess higher

Enter your guess: 25
Guess higher

Enter your guess: 24
Guess higher

Enter your guess: 27
Guess higher

Enter your guess: 28
Guess higher

Enter your guess: 29
You got It!
As promised here is your reward
You've recieved the Kokiri Sword!
Remember this land is very dangerous
That sword you now yield shall protect you!
You took30guessess

----jGRASP: operation complete.

Answer Source

Use another variable to increment the count of guesses as -

int guessInt = 0;
int guesses = 0; // this would track the count of guesses

while( guessInt != rand) {
  System.out.print( "Enter your guess: " );
  guessInt = input.nextInt();
  guesses++;

  if(guessInt > rand) {
    System.out.println("Guess lower");
    System.out.println();
  }
  if(guessInt < rand) {
   System.out.println("Guess higher");
   System.out.println();
  }
}

....
System.out.println("You took" + guesses + "guessess");
...

Note - You can clean your code by avoiding these many S.outs and using \n effectively.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download