meta master
Smash Journeyman
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)
EDIT: i added the code tags
(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