Created
September 14, 2018 02:52
-
-
Save landjd19/44d6d786ab8f3fa1e22bd2533db0735a to your computer and use it in GitHub Desktop.
Guessing Game Java Project
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
/** | |
* Random number generator that allows user three guesses as to which number 1-10 was generated. | |
* | |
* Jake Landaiche (your name) | |
* 9/13/18 (a version number or a date) | |
*/ | |
import java.util.*; | |
public class GuessingGame { | |
public static void main(String[] args) { | |
int maxNum = 10; | |
int minNum = 1; | |
boolean gameStarted = true; | |
boolean guessState = false; //false means the number has not been guessed correctly, true means the game is over whether win or lose | |
int guesses = 0; | |
boolean validInput = false; | |
Scanner sc = new Scanner(System.in); | |
int guessNum = 0; | |
int continueAnswer = 0; | |
while(gameStarted == true){ | |
System.out.println("This is the one and only guessing game"); | |
System.out.println("Please guess a number between 1 and 10"); | |
int randomNum; | |
randomNum = (int)(Math.random() * ((maxNum - minNum) + 1)) + minNum; //inclusive | |
//System.out.println(randomNum); | |
while(guessState == false){ | |
while(validInput == false) | |
try{ | |
guessNum = sc.nextInt(); | |
validInput = true; | |
}catch(Exception e){ | |
System.out.println("You didn't enter a number: " + e); | |
guessState = true; | |
validInput = true; | |
} | |
if(guessNum==randomNum){ | |
System.out.println("You Guessed " + guessNum + "!"); | |
System.out.println("You Win!!"); | |
gameStarted = false; | |
guessState = true; | |
validInput = false; | |
} else { | |
System.out.println("You Guessed " + guessNum + "!"); | |
System.out.println("You Lose..."); | |
guesses ++; | |
validInput = false; | |
if(guesses == 3){ | |
gameStarted = false; | |
guessState = true; | |
validInput = false; | |
} | |
} | |
} | |
if(guessState == true){ | |
System.out.println("Would You Like to Play Again? Press 1 to Continue, or Press 2 to Exit."); | |
sc.nextLine(); | |
continueAnswer = sc.nextInt(); | |
if(continueAnswer == 1){ | |
guessState = false; | |
gameStarted = true; | |
guesses = 0; | |
validInput = false; | |
} else { | |
guessState = false; | |
gameStarted = false; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment