Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Basic Guessing Game

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Basic Guessing Game

    Hi all, I have created a basic guessing game as follows:

    package latesttasks;
    // Create a basic guessing game, user has to guess 1 - 10, system prints
    // higher, lower, or correct. When correct answer is met, tell the user how 
    // many attempts it took and ask if they would like to play again. 
    import java.util.Random;
    import java.util.Scanner;
     
    public class Task3 {
     
        public static void main(String[] args) {
     
            Scanner scan = new Scanner(System.in);
     
            Random rand = new Random();
     
            int number = rand.nextInt(10) + 1;
     
            int guess;
     
            System.out.println("Welcome to Dan's Guessing Game");
            System.out.println("Guess the number between 1 and 10");
            System.out.println("");
     
            guess = scan.nextInt();
     
            while (guess < number) {
                System.out.println("Higher!");
                guess = scan.nextInt();
            }
            while (guess > number) {
                System.out.println("Lower!");
                guess = scan.nextInt();
            }
            while (guess == number) {
                System.out.println("Correct!");
                break;
            }
        }
    }


    Now what i want to do is take this program to the next level.

    Firstly I want the Program to count the user of attempts it took the user to guess correctly and print out a message like.... ''It took you 'x' attempts''

    Secondly I want to add an option for the user to play again at the end, for example print: '' Would you like to play again? y / n. The user would type 'y' to play again, or 'n' to terminate the program.

    Any suggestions ?

    --- Update ---

    Can someone please advise how to wrap my code in color coded tags rather than just plain ?

    Many thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Basic Guessing Game

    to count the user of attempts
    Define a variable to hold the count and add one to it each at each attempt.

    option for the user to play again
    Put the code in a while() loop. At the end of the code inside the loop, ask the user if he wants to repeat. If he says no exit the loop.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Guessing Game
    By Spark2.0 in forum Object Oriented Programming
    Replies: 4
    Last Post: June 6th, 2012, 12:14 PM
  2. Guessing game help
    By np657 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 12th, 2012, 07:39 AM
  3. guessing game
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2011, 02:30 PM
  4. guessing game assignment
    By scottey313 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 22nd, 2011, 07:52 PM
  5. Guessing Game
    By scottey93 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 7th, 2011, 02:50 PM