• Welcome to Smashboards, the world's largest Super Smash Brothers community! Over 250,000 Smash Bros. fans from around the world have come to discuss these great games in over 19 million posts!

    You are currently viewing our boards as a visitor. Click here to sign up right now and start on your path in the Smash community!

Java Guessing Game Help

meta master

Smash Journeyman
Joined
Jun 18, 2008
Messages
336
Location
Lewis Center, OH
for my computer class, we have to write a guessing game that creates a random number from 1-10 and then allows you 3 guesses. Any ways i can make this code more efficent?
(and yes, i had to add some of the comments)



Code:
 /** Program guessinggame
 * This program will create a random int from 1-10.
 * The user is to guess the numer within 3 tries.
 **/
import java.util.*;
public class guessinggame 
{
	public guessinggame()
	{
	}
	
	public static void main (String[] args)
	{
		int number = (int)(Math.random()*10 + 1); 
		Scanner input = new Scanner(System.in);
		System.out.println("I am thinking of a number from 1 to 10.");
		System.out.println("Can you guess it in three tries?");
		//start guessing
		System.out.println("Enter a guess: ");
		int guess = input.nextInt(); //created and sets the guess
		int count = 1; //number of times asked
		while (guess != number) 
		{
			count++; //incriment the count
			System.out.println("Wrong! Enter Guess " +count +": "); //Ask again
			guess = input.nextInt(); //resets the guess
			if (count==3 && guess!=number)
			{
				System.out.println("You lose! The number was " + number + ".");
				break;  // if the count has reached three and the number still hasn't been reached, then the loop will break
			}
		} //end while loop
		
		//You Win Message
		if (count==3 && guess==number)
			System.out.println("You Win!! The number was " + number + ".");
		else if (count==2 && guess==number) 
			System.out.println("You Win!! The number was " + number + ".");
		else if (count==1 && guess==number) 
			System.out.println("You Win!! The number was " + number + ".");
	} //End Main Method
} //End of Program guessinggame
EDIT: i added the code tags
 

TheBuzzSaw

Young Link Extraordinaire
Moderator
BRoomer
Joined
Jul 21, 2005
Messages
10,479
Put your code into the code BB tags to make it more readable.
 

Kirby King

Master Lameoid
Premium
BRoomer
Joined
Feb 8, 2002
Messages
7,577
Location
Being a good little conformist
Code:
/** Program guessinggame
 * This program will create a random int from 1-10.
 * The user is to guess the numer within 3 tries.
 **/
import java.util.*;
public class guessinggame 
{
    public guessinggame()
    {
    }
    
    public static void main (String[] args)
    {
        int number = (int)(Math.random()*10 + 1); 
        Scanner input = new Scanner(System.in);
        System.out.println("I am thinking of a number from 1 to 10.");
        System.out.println("Can you guess it in three tries?");
        //start guessing
        System.out.println("Enter a guess: ");
        int guess = input.nextInt(); //created and sets the guess
        int count = 1; //number of times asked
        while (guess != number) 
        {
            count++; //incriment the count
            System.out.println("Wrong! Enter Guess " +count +": "); //Ask again
            guess = input.nextInt(); //resets the guess
            if (count==3 && guess!=number)
            {
                System.out.println("You lose! The number was " + number + ".");
                break;  // if the count has reached three and the number still hasn't been reached, then the loop will break
            }
        } //end while loop
        
        //You Win Message
        if ([B]count==3[/B] && guess==number)
            System.out.println("You Win!! The number was " + number + ".");
        else if ([B]count==2[/B] && guess==number) 
            System.out.println("You Win!! The number was " + number + ".");
        else if ([B]count==1[/B] && guess==number) 
            System.out.println("You Win!! The number was " + number + ".");
    } //End Main Method
} //End of Program guessinggame
What's the point of the bolded checks? In other words, why do you need three checks there (when would guess == number and count not be 1, 2, or 3)?
 

TheBuzzSaw

Young Link Extraordinaire
Moderator
BRoomer
Joined
Jul 21, 2005
Messages
10,479
The losing line needs to be changed to say EPIC PHAILURE!
 

Kirby King

Master Lameoid
Premium
BRoomer
Joined
Feb 8, 2002
Messages
7,577
Location
Being a good little conformist
Let me push you one step further. Can you think of a case where (count > 3 && guess == number) is true?

Also, style note: I'd use fewer comments-on-the-same-line-as-the-code-I'm-writing. They're generally easier to read when they're just left above the code they're commenting, even though same-line comments can be more convenient other times. You definitely don't want to write comments like that most of the time.
 

MyCurse4Life

Smash Ace
Joined
Jan 6, 2009
Messages
552
Location
Los Angeles, California
Ugh, how can you people read this? Are you guys computer freaks because I too would really like to read this kind of stuff if my computer goes crazy for a reason and I always have to go to the hard drive and it takes me to stuff like this.
 

SativaJoe

Smash Rookie
Joined
Apr 1, 2008
Messages
21
Location
Fargo, ND
Ugh, how can you people read this? Are you guys computer freaks because I too would really like to read this kind of stuff if my computer goes crazy for a reason and I always have to go to the hard drive and it takes me to stuff like this.
Man I hate when I'm writing C++ and I have to go to the hard drive.
 
Top Bottom