How to count the amount of inputs the user inputs through a scanner in Java? - Overclock.net - An Overclocking Community

Forum Jump: 

How to count the amount of inputs the user inputs through a scanner in Java?

 
Thread Tools
post #1 of 2 (permalink) Old 10-25-2016, 08:31 PM - Thread Starter
New to Overclock.net
 
LearningJava's Avatar
 
Join Date: Oct 2016
Posts: 1
Rep: 0
I am doing an assignment that asks to create a guessing game but I have already coded the guessing game. I need to count the amount of attempts the user did when finding the correct answer to the guessing game. I am not sure what to use to count each attempt.

Here is my code for the Guessing game:
Code:
import java.util.Random;
    import java.util.Scanner;
    public class ThreeEleven
    {
   

     public static void main (String[] args)
        {
    final int MAX = 100;
    int answer, guess;
    String again;
    Scanner scan = new Scanner(System.in);
    System.out.print ("I'm thinking of a number between 1 and "
    + MAX + ". Guess what it is: (or enter 0 to quit) ");
    guess = scan.nextInt();
    
    Random generator = new Random(); //Random generator. 1 to 100.
    answer = generator.nextInt(MAX) +1;

        //if (guess == answer){ //If guess equals answer
            //System.out.println ("You got it! Good guessing!");

        //}
        //else if (guess == 0){ //Game ends
            //System.out.println ("You have ended your game. Goodbye.");

        //}
        
            while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue.

                if (guess > answer && guess != 0){ //If guess is higher than answer
                    System.out.println ("You guessed too high!");
                    guess = scan.nextInt();

                }
                else{
                    if (guess < answer && guess != 0){ //If guess is lower than answer
                        System.out.println ("You guessed too low!");
                        guess = scan.nextInt();
                    }

                    else if (guess == answer){ //If guess equals answer
                        System.out.println ("You got it! Good guessing!");
                    }
                    else if (guess == 0){ //Game ends
                        System.out.println ("You have ended your game. Goodbye.");
                    }
                        }
    }
            if (guess == answer){
                System.out.println ("You got it! Good guessing!");
               //System.out.println ("You guessed " +  + " times");
            }
    }

Output:
Code:
I'm thinking of a number between 1 and 100. Guess what it is: (or enter 0 to quit) 50
    You guessed too high!
    40
    You guessed too high!
    30
    You guessed too high!
    20
    You guessed too high!
    10
    You guessed too high!
    1
    You guessed too low!
    5
    You got it! Good guessing!
The user attempted to guess 7 times.
Would i do something like this?
Code:
System.out.println ("You guessed " + ? + " times");

If it is, what would I put where the "?" is?

Thank you in advance
LearningJava is offline  
Sponsored Links
Advertisement
 
post #2 of 2 (permalink) Old 10-26-2016, 06:09 AM
New to Overclock.net
 
Skyl3r's Avatar
 
Join Date: Nov 2012
Location: NE USA
Posts: 744
Rep: 27 (Unique: 23)
The most straight forward solution would be to define a variable to hold the total count of guesses by the user outside of the scope of the while loop. This would look something like this:

Code:
int userGuesses = 0; //Define a variable to hold guesses
while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue.

        if (guess > answer && guess != 0){ //If guess is higher than answer
                System.out.println ("You guessed too high!");
                guess = scan.nextInt();
                userGuesses ++; //Increment the guess count
        } else {
                if (guess < answer && guess != 0){ //If guess is lower than answer
                        System.out.println ("You guessed too low!");
                        guess = scan.nextInt();
                        userGuesses ++; //Increment the guess count
                } else if (guess == answer){ //If guess equals answer
                        System.out.println ("You got it! Good guessing!");
                } else if (guess == 0){ //Game ends
                        System.out.println ("You have ended your game. Goodbye.");
                }
        }
}

if (guess == answer){
    System.out.println ("You got it! Good guessing!");
    System.out.println ("You guessed " + userGuesses + " times"); //Print guesses
}
Skyl3r is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off