Reputation: 63
I'm attempting to add a counter to my program to count the number of guesses that the user has taken. I'm not sure how to do this...
Below is my code:
import java.util.*;//imports the utilities
public class WordPyramid {
public static void main(String[] args) {
int n;
Scanner kb = new Scanner(System.in);
System.out.println("Making 3 random numbers…");
System.out.println("What is the sum?");
Random rn =new Random();
int answer = rn.nextInt(10) + 1;
int answera=rn.nextInt(10) + 1;
int answerb=rn.nextInt(10) + 1;;
int fans =answer+answera+answerb;
while ((n = kb.nextInt()) != fans) {
System.out.println("Sorry, try again");
System.out.println("What is the sum?");
}
System.out.println("");
}
}
Upvotes: 0
Views: 1382
Reputation: 3386
Consider having a new int count = 0
outside your while
loop, and using an increment like count++
inside your loop.
This will increase your value for count
by 1 with each incorrect guess, or effectively each time the while
loop is run through.
You may find this informative as well, along with the rest of the Java Tutorials.
Upvotes: 1
Reputation: 8334
if you want to show every try number , here is a simple example :
int i=0;
while ((n = kb.nextInt()) != fans) {
i++;
System.out.println("Try number : "+i);
System.out.println("Sorry, try again");
System.out.println("What is the sum?");
}
Upvotes: 0