I want to make a game kind of like those Oregon Trail games, and everything goes fine, until the User actually has to input something, like if they want to hunt, or shop, or move on. The console prints the messages 3 times, but no error is shown. The message is supposed to print once, and I cannot figure out why this is happening.
Console:
[1]: https://i.stack.imgur.com/NMC4C.png
Code:
class Started{
static int DaysPassed = 1;
static double Food = 100;
static boolean city = true;
public void start()
throws java.io.IOException{
System.out.println("You have passed " + DaysPassed + " days");
System.out.println("You have " + Food + " food left");
if(city = true){
System.out.println("There is a city here");
}
System.out.println("Press M, H, or T to shop, hunt or travel respectively");
char decision = (char) System.in.read();
switch(decision){
case 'M':
if (city = true){
//open market interface
DaysPassed = DaysPassed + 1;
Food = Food - 10;
} else {
System.out.println("There is no city here, try next route");
}
break;
case 'H':
//open hunt interface
Food = Food - 10;
DaysPassed = DaysPassed + 1;
double hunter = Math.floor(Math.random());
System.out.println("You got" + hunter * 10 + " food");
Food = Food + hunter * 10;
System.out.println("Your food is now " + Food);
start();
break;
case 'T':
DaysPassed = DaysPassed + 1;
Food = Food - 10;
start();
break;
default:
System.out.println("Not a valid Character, remember that it has to be caps or it won't work");
start();
}
}
}
public class CalifornianTrail {
public static void main(String[] args)
throws java.io.IOException{
//some filler start code
System.out.println("Welcome to Californian Trail, a Console based video game based on Oregon Trail");
System.out.println("Starting Simulation");
System.out.println("Game does not have a save function");
System.out.println("This is game version 1.0 Alpha");
System.out.println("Today is the day you start your new journey as a pioneer.");
Started Menu = new Started();
Menu.start();
}
}
It was a long time since I worked with console programs, so I had to use the debugger this time.
This line is the reason:
char decision = (char) System.in.read();
and should be replaced with these two:
Scanner sc = new Scanner(System.in);
char decision = sc.next().charAt(0);
Why was your code executed three times?
Because when you type a char and press enter, a string like this is created M\n, so you have two chars inside the stream System.in. Each System.in.read() reads the first char after the last char that was read. You had two chars each time, so your code was executed three times.
Additionally, your code has a recurrence (the start method calls the start method). After a long time, you will run out of memory. The simplest way to avoid recurrence is to use a loop instead. A break command or return command can break it.
Please learn asap:
How to use a debugger
Naming conventions in java
On a game I am making, I have 4 doors, all with if's and else if's. But the code only allows for the first two doors, I don't know why exactly?
EDIT: VERY SORRY! I ASKED THE WRONG QUESTION. HERE IS THE REPHRASED:
The i1 is the input. i1==1 and i1==2 works, but i1==3
and i1==4 causes a break in the software, what is the issue?
Here is the complete code:
/**
* Write a description of class GameE02 here.
*
* #Aakarsh
* #1.0 (18 October 2015)
*/
import java.util.*;
//System.out.print('\f'); clear everything.
public class GameE02
{
public static void main(String [] args) throws InterruptedException{
Scanner reader = new Scanner(System.in);
int i1, i2, i3, i4, i5, i6, i7, i8, i9;
int o1, o2, o3, o4, o5, o6, o7, o8, o9;
String s1, s2, s3, s4, s5, s6, s7, s8, s9;
boolean loop = false;
boolean thugloop = false; //thug hop scotch loop
int counter = 1; //staircase counter
int counter2 = 1; //thug hopscotch counter
//somewhere, add in a never ending loop cycle (game over basically). Secret code required to end the game.
System.out.println("Do you want to play a game? (y = yes, n = no)");
s1 = reader.next();
System.out.println("You walk into a room, there are 5 doors on the left one room on the right, you look at the room; do you wish to enter?");
s2 = reader.next();
//Begin the stairchase choice - Step 1 of the game.
if(s2.equals("y")){
Thread.sleep(1000);
System.out.println("You have entered the room, you see a spiral staircase going up or chance to exit; you look at the staircase, do you wish to enter?");
s3 = reader.next();
Thread.sleep(1000);
loop = true;
System.out.println("You walk up the stairs, but you shockingly see yourself back in the same room, same position, climb again?");
s3 = reader.next(); //begin the staircase loop
while(loop==true){
if(s3.equals("y")){
System.out.println("You walk up the stairs, but you shockingly see yourself back in the same room, same position, climb again?");
s3 = reader.next();
counter++;
}else if(s3.equals("n")){
System.out.println("You have chosen not to climb the staircase.");
loop=false;
break;
}
} //loop ends here.
Thread.sleep(1000);
System.out.println("You are frightened and you walk back and see six doors, pick one (1, 2, 3, 4, 5, 6)");
//pun - pick one
s5=reader.next();
if(s5.equals("1")){
System.out.println("Good, you understood the pun; you may still have a chance at surviving.");
Thread.sleep(1000);
//add in the rest of the code here.
}else{
System.out.println("Damn, you really suck at reading - what are you, an AP Computer Science student? I said: 'six doors.. pick ONE...");
Thread.sleep(3950);
System.out.println("Since you lost that, I need to put you in the maze, sorry!");
}
} if(s2.equals("n")){
counter=0;
Thread.sleep(1000);
System.out.println("You see four doors, which one do you pick? (1, 2, 3, 4)");
i1 = reader.nextInt(); //door choice number.
if(i1 == 1){
Thread.sleep(500);
System.out.println("You go inside a dungeon, and meet a skeleton there; ");
Thread.sleep(1000);
System.out.println("You talk to the skeleton; Press 'y' if you want to play thug hop-scotch, or 'n' if you want to fight.");
s4=reader.next();
if(s4.equals("y")){
Thread.sleep(1000);
System.out.println("Enter some 5 (after this) from 1 to maximum-17 (inclusive) individually. Repeats are allowed.");
i4=reader.nextInt();
i5=reader.nextInt();
i6=reader.nextInt();
i7=reader.nextInt();
i8=reader.nextInt();
int sumhop = i4 + i5 + i6 + i7 + i8;
System.out.println("Through a complex algorithm your sequence of numbers read " + sumhop);
Thread.sleep(3700);
System.out.println("Fortunately (or unforunately) the thug you were playing hopscotch against was confident so he had this program generate him a random number.");
Thread.sleep(2500);
int randomhop = 3 + (int)(Math.random()*84); //thug hop scotch random.
System.out.println("His random number generated was: " + randomhop);
Thread.sleep(1000);
if(sumhop < randomhop){
System.out.println("Seriously? READ CAREFULLY - When asking the question, I told you: 'Enter 5 SUM-numbers-MAXIMUM'.");
Thread.sleep(3000);
System.out.println("You should have done 17*5 = 85 to outbeat the thug");
Thread.sleep(3000);
System.out.println("Anyway, time to do battle with the thug skeleton!");
counter2=0;
} else if(sumhop > randomhop){
System.out.println("Woah, you beat the thug; but now the thug is angry and he won't rest until he beats you.");
Thread.sleep(2000);
thugloop = true;
System.out.println("Pick your numbers again");
o4=reader.nextInt();
o5=reader.nextInt();
o6=reader.nextInt();
o7=reader.nextInt();
o8=reader.nextInt();
int sumhop2 = o4+o5+o6+o7+o8;
int angrythug = 3 + (int)(Math.random()*85);
while(thugloop == true){
if(sumhop2>angrythug){
counter2++;
Thread.sleep(1000);
System.out.println("The thug picked: " + angrythug + " .Woah, you are really good at this!");
Thread.sleep(1500);
System.out.println("But the thug is getting angrier. Pick again!");
o4=reader.nextInt();
o5=reader.nextInt();
o6=reader.nextInt();
o7=reader.nextInt();
o8=reader.nextInt();
sumhop2 = o4+o5+o6+o7+o8;
angrythug = 3 + (int)(Math.random()*85);
}else if(sumhop2<angrythug){
thugloop = false;
break;
}
}
System.out.println("Aha, the thug picked: " + angrythug + " -- you couldn't outdo the thug, could you? Well, it was a good run; time to do battle with the skeleton.");
Thread.sleep(3000);
s4="n";
}
} if(s4.equals("n")){
Thread.sleep(1000);
System.out.println("Now -- it is time to fight the skeleton");
Thread.sleep(2000);
System.out.println("Your survival depends on your mathematics skills.");
Thread.sleep(2700);
System.out.println("Type in your answers one-by-one.");
Thread.sleep(2500);
System.out.println("By the way, Fetty Wap's song six-seven-nine is missing something.");
Thread.sleep(3500);
System.out.println("(1) What is the highest power of 2 that divides 10! (factorial)");
Thread.sleep(4000);
System.out.println("(2) You write the numbers from 1... onwards like: 123456789(10)(11)..(643)..(707)..(984)... What is the number that starts on the 2013th digit of this sequence?" );
int a1 = reader.nextInt();
int a2 = reader.nextInt();
int suma = a1+a2;
if(suma==715){
System.out.println("Wow, you're pretty damn smart; you beat the skeleton and all the odds. You might have won the game, all there is left is one question. READ CAREFULLY.");
Thread.sleep(5000);
System.out.print('\f');
System.out.println("Read carefully: Write your answers AFTER ALL QUESTIONS APPEARED;");
System.out.println("I hope you remembered everything; one-by-one, in order- write how many times you:");
System.out.println("(1) Walked up the stairs (including loops, everything)");
System.out.println("(2) How many times you beat the thug at hopscotch (including loops etc..)");
int f1, f2;
f1 = reader.nextInt();
f2 = reader.nextInt();
int sumf = f1+f2;
if(sumf==counter+counter2){
System.out.println("Congratulations, you have beaten the odds and won the game. Now, do something worthwhile =)");
System.exit(0);
}
}
}
} if(i1 == 2){
Thread.sleep(500);
System.out.println("You go inside a dungeon, and meet a skeleton there; ");
Thread.sleep(1000);
System.out.println("You talk to the skeleton; Press 'y' if you want to play thug hop-scotch, or 'n' if you want to fight.");
s4=reader.next();
if(s4.equals("y")){
Thread.sleep(1000);
System.out.println("Enter some 5 (after this) from 1 to maximum-17 (inclusive) individually. Repeats are allowed.");
i4=reader.nextInt();
i5=reader.nextInt();
i6=reader.nextInt();
i7=reader.nextInt();
i8=reader.nextInt();
int sumhop = i4 + i5 + i6 + i7 + i8;
System.out.println("Through a complex algorithm your sequence of numbers read " + sumhop);
Thread.sleep(3700);
System.out.println("Fortunately (or unforunately) the thug you were playing hopscotch against was confident so he had this program generate him a random number.");
Thread.sleep(2500);
int randomhop = 3 + (int)(Math.random()*84); //thug hop scotch random.
System.out.println("His random number generated was: " + randomhop);
Thread.sleep(1000);
if(sumhop < randomhop){
System.out.println("Seriously? READ CAREFULLY - When asking the question, I told you: 'Enter 5 SUM-numbers-MAXIMUM'.");
Thread.sleep(3000);
System.out.println("You should have done 17*5 = 85 to outbeat the thug");
Thread.sleep(3000);
System.out.println("Anyway, time to do battle with the thug skeleton!");
counter2=0;
} else if(sumhop > randomhop){
System.out.println("Woah, you beat the thug; but now the thug is angry and he won't rest until he beats you.");
Thread.sleep(2000);
thugloop = true;
System.out.println("Pick your numbers again");
o4=reader.nextInt();
o5=reader.nextInt();
o6=reader.nextInt();
o7=reader.nextInt();
o8=reader.nextInt();
int sumhop2 = o4+o5+o6+o7+o8;
int angrythug = 3 + (int)(Math.random()*85);
while(thugloop == true){
if(sumhop2>angrythug){
counter2++;
Thread.sleep(1000);
System.out.println("The thug picked: " + angrythug + " .Woah, you are really good at this!");
Thread.sleep(1500);
System.out.println("But the thug is getting angrier. Pick again!");
o4=reader.nextInt();
o5=reader.nextInt();
o6=reader.nextInt();
o7=reader.nextInt();
o8=reader.nextInt();
sumhop2 = o4+o5+o6+o7+o8;
angrythug = 3 + (int)(Math.random()*85);
}else if(sumhop2<angrythug){
thugloop = false;
break;
}
}
System.out.println("Aha, the thug picked: " + angrythug + " -- you couldn't outdo the thug, could you? Well, it was a good run; time to do battle with the skeleton.");
Thread.sleep(3000);
s4="n";
}
} if(s4.equals("n")){
Thread.sleep(1000);
System.out.println("Now -- it is time to fight the skeleton");
Thread.sleep(2000);
System.out.println("Your survival depends on your mathematics skills.");
Thread.sleep(2700);
System.out.println("Type in your answers one-by-one.");
Thread.sleep(2500);
System.out.println("By the way, Fetty Wap's song six-seven-nine is missing something.");
Thread.sleep(3500);
System.out.println("(1) What is the highest power of 2 that divides 10! (factorial)");
Thread.sleep(4000);
System.out.println("(2) You write the numbers from 1... onwards like: 123456789(10)(11)..(643)..(707)..(984)... What is the number that starts on the 2013th digit of this sequence?" );
int a1 = reader.nextInt();
int a2 = reader.nextInt();
int suma = a1+a2;
if(suma==715){
System.out.println("Wow, you're pretty damn smart; you beat the skeleton and all the odds. You might have won the game, all there is left is one question. READ CAREFULLY.");
Thread.sleep(5000);
System.out.print('\f');
System.out.println("Read carefully: Write your answers AFTER ALL QUESTIONS APPEARED;");
System.out.println("I hope you remembered everything; one-by-one, in order- write how many times you:");
System.out.println("(1) Walked up the stairs (including loops, everything)");
System.out.println("(2) How many times you beat the thug at hopscotch (including loops etc..)");
int f1, f2;
f1 = reader.nextInt();
f2 = reader.nextInt();
int sumf = f1+f2;
if(sumf==counter+counter2){
System.out.println("Congratulations, you have beaten the odds and won the game. Now, do something worthwhile =)");
System.exit(0);
}
}
} if(i1 == 3){
Thread.sleep(1);
System.out.println("Oooooooooh... you picked the maze - good luck");
Thread.sleep(2000);
System.out.println("You stepped on a trap and fall in a dungeon. One way you see the light, the other, darkness. Press 'y' for light-way and 'd' for dark-way.");
String dun = reader.next();
if(dun.equals("y")){
System.out.println("Good you saw the light, now you see a white board with a math question. Press 'y' to proceed or 'n' to go to the dark side.");
String pro = reader.next();
if(pro.equals("y")){
System.out.println("You have chosen to answer the math question.... smart choice.");
Thread.sleep(2000);
System.out.println("what is 2^2?");
int ans = reader.nextInt();
Thread.sleep(7700);
System.out.println("You really thought it would be that easy?");
Thread.sleep(1000);
System.out.println("You are given a 12th degree polynomial is the form: P(x) = 23x^(12) + ax^(11) + ... + 8");
Thread.sleep(1500);
System.out.println("The roots are respectively: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12");
Thread.sleep(3000);
System.out.println("What is the absolute value of the coefficient 'a' in the poylnomial P(x)?");
int poly = reader.nextInt();
if(poly==161){
System.out.println("You have done what many others never could. Now do something worthwhile!");
System.exit(0);
}
} else if(pro.equals("n")){
dun="n";
}
} if(dun.equals("n")){
System.out.println("Okay, you chose the dark side - what is the first name of the 4th US President? (No caps)");
String res = reader.next();
if(res.equals("james")){
System.out.println("Nice. Suddenly, an earthquake appears and you fall down the dungeon.");
Thread.sleep(1500);
System.out.println("You are stuck with a skeleton who wants to play thug hopscotch really badly; you must play - get ready!");
Thread.sleep(4300);
}
} if(i1 == 4){
System.out.println("Huh - smart guy, you won the game.");
System.exit(0);
}
}
}
}
}
}
There is the complete code; please help me out!
Your braces are "missalligned". You are closing the if (i1 == 2) block in the wrong place, and it encapsulates the checks for 3 and 4.
Your IDE should be able to highlight matching braces to help you see where the block ends.
You can use the switch-case construct if you have learned it, it usually makes this type of job easier.
So I wrote a java code for a numbers guessing game. The entire thing is pretty much done. It works by choosing a random number then asking the user for console inputs and then saying whether that is higher or lower than the random number. Once you guess it, it then asks if you want to play again. When you finally say no to this (be it one game or several) it prints out your Overall results including total games, total guesses, avg guesses/game and your best game. I have everything worked out except I cant figure out how to make it print your overall best game.
import java.util.*; //so I can use scanner
public class GuessingGame {
public static void main(String[] args) {
Random rand = new Random ();
int max = 100;
Scanner input = new Scanner(System.in);
int guess;
boolean play = true;
int totalGames = 0;
int totalGuesses = 0;
System.out.println("Can you guess the word?");
System.out.println("I am sure you cannot guess!");
System.out.println("Go ahead and try!");
System.out.println();
while (play) {
System.out.println("I'm thinking of a number between 1 and " + max + "...");
int numberToGuess = rand.nextInt(max) + 1;
int numberOfTries = 0;
boolean win = false;
while (!win) {
System.out.print("Your guess? ");
guess = input.nextInt();
numberOfTries++;
if (guess == numberToGuess) {
win = true;
} else if (guess > numberToGuess) {
System.out.println("It's lower.");
} else if (guess < numberToGuess) {
System.out.println("It's higher.");
}
input.nextLine();
}
if (numberOfTries == 1) {
System.out.println("You got it right in " + numberOfTries + " guess!");
} else {
System.out.println("You got it right in " + numberOfTries + " guesses!");
}
totalGames++;
totalGuesses+= numberOfTries;
System.out.print("Do you want to play again? ");
String answer = input.nextLine();
char firstLetter = answer.charAt(0);
if (firstLetter == 'y' || firstLetter == 'Y') {
play = true;
} else {
play = false;
}
System.out.println();
}
System.out.println("Overall results:");
System.out.println("Total games = " + totalGames);
System.out.println("Total guesses = " + totalGuesses);
System.out.println("Guesses/game = " + totalGuesses/totalGames);
System.out.println("Best game = ");
}
}
In order to get the best game you need a keep track of the best best after each game, such as a variable that checks it there is a new best game after each game.
Keep track of the best score, which is the lowest number of guesses.
int bestGame = Integer.MAX_VALUE; // at the top
bestGame = Math.min(bestGame, numberOfTries); // at the end of your inner while loop
The worst possible score is the highest number of guesses, which is limited by Integer.MAX_VALUE, so you start there.
By the best game u mean minimum number of tries needed to answer is the best game.
/* int mintries,bestgame,gamenumber=0;
bestgamenumber=0;mintreies=Integer.MAX_VALUE:*/
Add the above lines above your while(play)
gamenumber++;
/*if(mintries>numberOfTries)
{
mintries=numberOfTries;//update mintries
betgame=gamenumber;
}*/
Add the if condition just before closing while(play).
So it will be like
int mintries;
mintreies=Integer.MAX_VALUE:
int gamenumber=0;
int bestgamenumber=0//if you want to print the which game is the best game(!st,2nd,3rd..) ;
while(play)
{
// do all your stuff
gamenumber++;
if(mintries>numberOfTries)
{
mintries=numberOfTries;//update mintries
bestgamenumber=gamenumber;
}
}
}
System.out.println("Game number +bestgamenumber+"was the best game with"+ mintries+"tries);
I am considering that you want to print which game (1st,2nd,3rd)is best and minimum tries made to guess the best game.Correct me if i am wrong.
To fit into the code you have already written, You could
Create a new 'global' variable, for example int bestGame = Integer.MAX_VALUE;.
Whenever the user is done with a game do a check if the current numberOfGuesses is smaller than the current bestGame, and if it is, then overwrite bestGame with the current numberOfGuesses.
At the end, you simply need to output bestGame.
I made this simple app where a player attacks a boss and the player has to tries to defeat the boss. What I am having trouble with is having my small java app to print after the boss has been defeated. My bosses' health is 5 and I have a random dice generator that ranges 1 - 6. When the player rolls "5" or a "6" on the first attack, I want the program to end. Another thing is that my program prints out I have defeated the boss even though the player has not dealt 5 or 6 of damage. I think it might be because of the for loop but I'm not sure.
Here is the code:
import java.util.Scanner;
import java.util.Random;
public class Hey{
public static void main (String args[]){
Scanner attack = new Scanner(System.in);
Random dice = new Random();
String hits;
int hitss,total = 0;
System.out.println("Time to attack the boss!");
System.out.println("Press letter 'A' to attack!");
hits = attack.next();
hitss = 1+dice.nextInt(6);
for(int i=0;i<2;i++){
if(hits.equals ("a")){
System.out.println("You have hit a " + hitss);
total += hitss;
}
if(total >= 5){
System.out.println("Well done, You have defeated the boss!");
}
else if(total < 5){
total = 5 - total;
System.out.println("You need " + total + " more hits to defeat the boss!");
}
else
{
System.out.println("Sorry, you did not defeat the boss");
}
}
}
}
Output:
You have hit a 2
You need 3 more hits to defeat the boss!
You have hit a 2
Well done, You have defeated the boss!
You didn't close the scanner.
Your test for not defeating the boss has to be outside the for loop.
You needed a different variable (more) to see what hits were left.
You needed to break out of the for loop if you won on the first roll.
Here's your code with my changes
import java.util.Scanner;
import java.util.Random;
public class Hey {
public static void main(String args[]) {
Scanner attack = new Scanner(System.in);
Random dice = new Random();
String hits;
int hitss, total = 0;
System.out.println("Time to attack the boss!");
System.out.println("Press letter 'A' to attack!");
hits = attack.next();
hitss = 1 + dice.nextInt(6);
for (int i = 0; i < 2; i++) {
if (hits.equals("a")) {
System.out.println("You have hit a " + hitss);
total += hitss;
}
if (total >= 5) {
System.out.println("Well done, You have defeated the boss!");
break;
} else {
int more = 5 - total;
System.out.println("You need " + more
+ " more hits to defeat the boss!");
}
}
if (total < 5) {
System.out.println("Sorry, you did not defeat the boss");
}
attack.close();
}
}
for the following code, I need to stop the code by typing the word "quit", but without using "break" or "system.exit" statements. anyone can help me out? I think boolean could solve this but i have no idea how to use it.
I placed the quit statement in the first two lines of the loop,
but im not sure if it belongs there. Im in my learning phase, so dont be too strict :))
import java.util.*;
public class Game {
public static void main (String[]args){
Game guessing = new Game();
guessing.start();
}
public void start() {
System.out.println("Welcome to guessing game!");
System.out.println("Please enter the number between 1 and 1000");
Scanner input = new Scanner(System.in);
String playerName;
String currentGuess;
String quit = "quit";
boolean playGame = true;
int tries = 0; //number of times player guessed
int guess = 0; //number that player inputs
long startTime = System.currentTimeMillis(); //start timer after first guess
int randomNumber = (int) (Math.random() * 999 + 1); // generating random number
System.out.println(randomNumber); // to be deleted after game is finished
currentGuess = input.nextLine();
do{
if (currentGuess.equalsIgnoreCase(quit)) {
System.out.println("Thanx for playing");}
if (currentGuess.matches("[0-9]+")) {
int PlayerGuessInt = Integer.parseInt(currentGuess);
}
else {
System.out.println("You have netered non-numeric value,please try again");
currentGuess = input.nextLine();
continue;
}
guess = Integer.parseInt(currentGuess);
if(guess<1 || guess>1000 ){
System.out.println("The number is out of range! Please try again");
currentGuess = input.nextLine();
continue;
}
if (guess>randomNumber){
System.out.println("Oops,too high!");
currentGuess = input.nextLine();
tries++;
}
else if (guess<randomNumber){
System.out.println("Sorry, to low!");
currentGuess = input.nextLine();
tries++;
}
}
while (guess!=randomNumber);
if (guess==randomNumber){
tries++;
long endTime = System.currentTimeMillis();
long gameTime = endTime - startTime;
System.out.println("Well done! You won the game in " + tries + " guesses " + "and " + gameTime/1000 + " seconds!");
System.out.println("Please enter your name: ");
playerName = input.nextLine();
}
}
}
Change your while loop so that it checks for the value of current guess being "quit". That way it will stop looping when the quit command is given e.g.
while(guess!=randomNumber && !currentGuess.equalsIgnoreCase(quit))
Put inside the while in one (or all of the if )
if (currentGuess.equals("quit") {
playGame = false
}
and change the while to be:
while (guess!=randomNumber && playGame)
Here's the answer:
Global boolean variable:
bool userWantsToQuit = false;
so if someone types int quit (shouln't it be "quit" because it's string?)
if (currentGuess.equalsIgnoreCase(quit))
{
userWantsToQuit = true; // we mark that someone wants to quit
System.out.println("Thanx for playing"); // we output message
continue; // skip execution of the rest (or you could use else if)
}
at the bottom you change while statement to stop the game if userWantToQuit is true:
(guess!=randomNumber && !userWantsToQuit)
It's been long time since I used java and I didn't test it, but it's definitely in good direction.