/**
* Guess a number from 1 - 10
*
* @author Sean Appaneal
* @version v1.0
*/
import java.util.*;
public class guessingGame
{
public static void main(String args[]){
int randNum; //randomnumber
//get random number
randNum = (int)(Math.random() * ((10 - 1) + 1)) + 1;
//guesses
int guess1 = 0;
int guess2 = 0;
int guess3 = 0;
//boolean for replaying after the game is over
boolean play = true;
while(play){
//scanner, and text asking for numbers
Scanner sc = new Scanner(System.in);
System.out.println("This is the one and only guessing game");
System.out.println(" "); //blank line to make it look nice
System.out.println("Please guess 3 numbers between 1 and 10");
guess1 = sc.nextInt();
if(guess1 > 10 || guess1 < 0){ //check to make sure guess is from 1 to 10
System.out.println("Please guess a number between 1 and 10");
guess1 = sc.nextInt();
}
if(guess1 == randNum){ //check if guess is equal to random number
System.out.println("You correctly guessed " + guess1 + " the random number was " +randNum);
}
//if statement for if guess1 is incorrect
if(guess1 != randNum ){
System.out.println("Incorrect, please enter a second guess");
guess2 = sc.nextInt();
if(guess2 > 10 || guess2 < 0){ //check to make sure guess is from 1 to 10
System.out.println("Please guess a number between 1 and 10");
guess2 = sc.nextInt();
}
if(guess2 == randNum){ //check if guess is equal to random number
System.out.println("You correctly guessed " + guess2 + " the random number was " +randNum);
}
//if statement for if guess 1 and 2 are incorrect
if(guess1 != randNum || guess2 != randNum){
System.out.println("Incorrect, please enter a third guess");
guess3 = sc.nextInt();
if(guess3 > 10 || guess3 < 0){ //check to make sure guess is from 1 to 10
System.out.println("Please guess a number between 1 and 10");
guess3 = sc.nextInt();
}
if(guess3 == randNum){ //check if guess is equal to random number
System.out.println("You correctly guessed " + guess3 + " the random number was " +randNum);
}
}
}
//print out the random number, and the user's guesses
if(guess1 != randNum && guess2 != randNum && guess3 != randNum){
System.out.println( "You incorrectly guessed: " +guess1 + ", " + guess2 + ", and " + guess3);
System.out.println(" "); //just a blank line to make it look nice
System.out.println( "The Random Number was: " + randNum + " , try again");
System.out.println(" "); //another blank line to make it look nice
}
//comparing guesses to randNum
System.out.println(" "); //blank line number 3: to make it look nice
//ask if they want to keep going or stop
System.out.println("Press 1 to play again, press 2 to stop" );
int go = sc.nextInt();
if(go != 1){
play = false; //stop game
}else{
System.out.println(" "); //fourth blank line to make it look nice
//regenerate random number, if this line is removed it uses the same number over and over again
randNum = (int)(Math.random() * ((10 - 1) + 1)) + 1;
}
}//end of while loop
}//end of main
}//end of class
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.