Jump to content

How To Make A Java Guessing Game?

piggykid1

I tried making my own and kept getting errors, so i was wondering how you guys do it.

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

Could you post your code please?

 

The basic stages would be:

 

1-generate a random number

2-ask user for input

3-if user input is not equal to the random number ask the user to enter another number

4-else if user input is equal to the right number tell the user they got the right number

 

Hope this helps.

 

Edit: I presumed you meant a guessing game with numbers. I'm also guessing this is with a gui and not console based.

Link to comment
Share on other sites

Link to post
Share on other sites

Have a guess!

But on a serious note:

We need something to work with if you want us to help you, show us what parts of the code are causing troubles, and what logic you're going after.

Link to comment
Share on other sites

Link to post
Share on other sites

"Post code so one can help you"  :blink:

 

Note that a simple guessing game can use simple if-else statements and any looping statements together with necessary variable declarations.

------------------------------------

     ~ Live Love Code ~

------------------------------------

Link to comment
Share on other sites

Link to post
Share on other sites

Here's one I made a while ago for a school project:

 

 

/**
 * Caspur
 * Oct 30, 2013
 * This program is a game where the computer picks a random number and the user must guess it
 * JDK 1.7.0
 */

import javax.swing.JOptionPane;

public class GuessingGame {
    
    public static void main(String[] args)
    {
    //Main method
    int computerNumber=(int) (Math.random()* 100+1);
    System.out.println("The correct guess would be " + computerNumber);
    String response=JOptionPane.showInputDialog(null,
        "Enter a guess between 1 and 100","Guessing Game", 3);
    int userAnswer=Integer.parseInt(response);
    JOptionPane.showMessageDialog(null, "Your guess is "
        + determineGuess(userAnswer, computerNumber));
    
    }
    
    public static String determineGuess (int userAnswer, int computerNumber) {
    if (userAnswer <= 0 || userAnswer > 100)
    {
        return "invaled";
    } else if (userAnswer == computerNumber)
    {
        return "correct";
    } else
    {
        return "incorrect";
    }
    
        
    }
    }

 

It does the game in a JPane and it outputs the correct number in the Output for testing reasons.

"Do not pray for and easy life, pray for the strength to endure a difficult one."

 

"A place where everything and nothing exists, this is my playground."

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×