How to create a Java guessing game
3,741 viewsA basic Java guessing game I made to show you how it could be done. You can specify the amount of numbers to guess and shows the total guesses you made to guess the correct number.
I've commented out the "continue" part because I'm too lazy to fix the small error that but if you uncomment the code you will see whats wrong. I suppose i'll fix that later.
Anyway here's the code:
-
import java.util.InputMismatchException;
-
import java.util.Scanner;
-
public class GuessGame {
-
String guessThisNumber = "";
-
int[] secretNumber ;
-
boolean guessedCorrectNumber = false, continueOrExitGame = false, debug = true;
-
int tries = 0, difficulty, rounds = 1;
-
//int difficulty;
-
GuessGame GG = new GuessGame();
-
GG.usage();
-
GG.playGame();
-
}
-
public void playGame(){
-
try{
-
difficulty = sc.nextInt();
-
this.generateSecretNumber(difficulty);
-
while(!this.guessedCorrectNumber){
-
if(guess.length()> difficulty || guess.length()
-
}
-
if(guess.length() == difficulty){
-
this.checkSecretNumber(guess);
-
}
-
this.tries++;
-
if(guess.equals(this.guessThisNumber)){
-
this.guessedCorrectNumber = true;
-
/*
-
Scanner quitGameChoice = new Scanner(System.in);
-
System.out.print("Again? Y/N: ");
-
String continueOrExit = quitGameChoice.nextLine();
-
while(!continueOrExitGame){
-
if(continueOrExit.equals("n") || continueOrExit.equals("N")){
-
continueOrExitGame = true;
-
System.out.println("Thanks for playing!");
-
System.exit(0);
-
}
-
if(continueOrExit.equals("y") || continueOrExit.equals("Y")){
-
continueOrExitGame = true;
-
guessedCorrectNumber = false;
-
tries = 0;
-
rounds++;
-
System.out.println("Alright! Ready for round " + rounds + "?");
-
System.out.println();
-
this.playGame();
-
}
-
else{
-
System.out.println("Please make a choice, again?: Y/N");
-
continueOrExit = quitGameChoice.nextLine();
-
}
-
}
-
*/
-
}
-
}
-
}
-
catch(InputMismatchException IME){
-
}
-
}
-
public void generateSecretNumber(int totalNumbers){
-
guessThisNumber = "";
-
secretNumber = new int[totalNumbers];
-
for (int i = 0; i <totalNumbers; i++){
-
secretNumber[i] = randomNumber;
-
}
-
for(int i = 0; i <secretNumber.length; i++){
-
guessThisNumber +=secretNumber[i];
-
//System.out.print(secretNumber[i]);
-
}
-
if(debug){
-
}
-
}
-
int[] compareNumber = new int[userInput.length()];
-
for (int i = 0; i <userInput.length(); i++){
-
compareNumber[i] = userInput.charAt(i);
-
}
-
/*
-
System.out.print(compareNumber[0]-48); // -48?
-
*/
-
for (int i = 0; i <userInput.length(); i++){
-
if(compareNumber[i]-48 <secretNumber[i]){
-
}
-
if(compareNumber[i]-48> secretNumber[i]){
-
}
-
if(compareNumber[i]-48 == secretNumber[i]){
-
}
-
}
-
}
-
public void usage(){
-
}
-
}
Output:
Welcome to the guessing game, guess the correct number!
By: HappyFace - www.engineeringserver.com :: v1.0 initial releaseLegenda:
+ means higher
- means lower
! means correctHow many numbers you want to guess? 4
Secret number is: 6303
Alright, the secret number(s) are generated!Guess the secret number: 6303
6! 3! 0! 3!Concratulations! the secret number is indeed 6303
Total tries: 1
//edit
Oh and before I forget to mention it.. to disable the secretcode that shows on the screen just change "debug = true" into "debug = false" and the secretcode won't be visible.












