Created
September 14, 2018 17:13
-
-
Save feehe21/1a31ac1ae09bebdec8ace083d19d7731 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
/**/ | |
public class guessingGame | |
{ | |
//global variable for cross-function use | |
static Scanner scan = new Scanner(System.in); | |
public static void main(String args[]){ | |
//variable declaration | |
int max = 10; | |
int min = 1; | |
int randomNum = (int)(Math.random() * ((max - min) + 1)) + min; | |
int tries = 0; | |
int guess; | |
int playAgain = 1; | |
boolean winner = false; | |
//start game | |
System.out.println("This is the one and only guessing game"); | |
System.out.println("Please guess a number between 1 and 10"); | |
while(playAgain == 1){ | |
while(tries<3){//limits tries | |
if(tries>0 && tries==2){//one more try | |
System.out.println("You have 1 more try"); | |
}else if(tries>0){//2 more tries | |
System.out.println("You have " + (3-tries) + " more tries"); | |
} | |
guess = guessError();//recieve input | |
if(guess == randomNum && guess>0 && guess<11){ | |
//if guessed correctly | |
System.out.println("You Win!! The number was " + randomNum); | |
System.out.println("Press 1 to play again or 2 to quit"); | |
playAgain = playAgainError(); | |
if(playAgain == 1){ | |
tries = 0; | |
System.out.println("New Game: Guess a number between 1 and 10"); | |
randomNum = (int)(Math.random() * ((max - min) + 1)) + min; | |
}else{ | |
tries = 3; | |
winner = true;//this is to skip the end of the program | |
} | |
}else if(guess<1 || guess>10){//if not within defined range | |
System.out.println("That is not between 1 and 10"); | |
System.out.println("Please enter a new number"); | |
}else{//if guessed incorrectly | |
System.out.println("Nope!"); | |
tries++; | |
} | |
} | |
if(winner == false){//if out of tries and lost | |
System.out.println("Too Bad, You Lost"); | |
System.out.println("The Number Was: " + randomNum); | |
System.out.println("Press 1 to play again or 2 to quit"); | |
playAgain = playAgainError(); | |
if(playAgain == 1){//restart | |
tries = 0; | |
System.out.println("New Game: Guess a number between 1 and 10"); | |
randomNum = (int)(Math.random() * ((max - min) + 1)) + min; | |
} | |
} | |
} | |
} | |
public static int guessError(){ | |
int guess=0; | |
boolean valid = false; | |
while(valid==false){ | |
try{ | |
valid = true; | |
guess = scan.nextInt(); | |
}catch(Exception e){ | |
System.out.println("That wasn't a number"); | |
valid=false; | |
} | |
scan.nextLine(); | |
} | |
return guess; | |
} | |
public static int playAgainError(){ | |
int tryAgain = 0; | |
boolean valid = false; | |
while(valid==false){ | |
try{ | |
valid = true; | |
tryAgain = scan.nextInt(); | |
}catch(Exception e){ | |
System.out.println("That wasn't a number"); | |
valid = false; | |
} | |
} | |
scan.nextLine(); | |
return tryAgain; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment