Hi everybody - need some help with this code. Been trying to create a simple gussing number game and wrote out this code but can't see what the problem is as there are some errors can someone help?

// This program asks the user to enter a value between 1 and 20.
// If the value entered by the user matches the previoslt random value generated
// by the program then the program displays how many goes the user had.
// Otherwise it reports if its is higher or lower. When the user guesses correctly the program
// asks the user if she or he wishes to continue by typing yes or no.

    /**
     * Sample output:
     * Enter a guess:
     * 12
     * guess lower
     * Enter a guess:
     * 6
     * correct with 2 guesses
     * Do you want to play again (Y,N)?
     * Y
     * Enter a guess:
     * 1
     * guess higher
     * Enter a guess:
     * 12
     * guess higher
     * Enter a guess:
     * 18
     * correct with 3 guesses
     * Do you want to play again (Y,N)?
     * n
     * @param args
     */

public static void main(String[] args) {
// TODO Auto-generated method stub

        int number;
        int turns;
        String s;
        char playAgain;
        Random Generator = new Random;

        do
        {
            number = Generator.nextInt(20)+1;
            turns = 0;
            int guess = 0;
            while ( guess != number )
            {
                System.out.println("Enter a guess: ");
                guess = Easyln.getInt();
                turns = turns + 1;
                if ( guess == number )
                System.out.println("correct with " + turns + "guesses");

                else if ( guess < number)
                    System.out.println ("guess higher");

                else
                    System.out.println("guess lower");
        }
        System.out.print("Do you want to play again (Y,N)?");
        s = Easyln.getString();
        playAgain = s.charAt(0);

        while ( playAgain == 'n' && playAgain == 'N' )

        }
    }
}

Recommended Answers

All 2 Replies

You don't have a class to put your methods in. That would be a good place to start. Also

Random generator = new Random;

It should say

Random generator = new Random();

Your while statement at the bottom doesn't do anything.

Thanks for the reply - been helpful.
Forgot to post the class -

import java.util.Random;

public class GuessMyNo {

There are still some errors but its nearly finished!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.