Need help fixing difficulty selection for guessing game in java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I need help making my guessing game give you the option to choose which difficulty, can someone fix my code and repost it because I'm kinda lost right now!
import java.util.*;
public class GuessingGame
{
public static void main(String[] args)
{
boolean play = true;
String input1;
while(play == true){
Scanner input = new Scanner(System.in);
Random rand = new Random();
int counter = 0;
int guess = -1;
int dif = 0;
int easy = rand.nextInt(10) + 1, med = rand.nextInt(1000) + 1, hard = rand.nextInt(10000) + 1;
System.out.println("Difficulty Select");
System.out.println("=================");
System.out.print("1) Easy 2) Medium 3) Hard :");
dif = input.nextInt();
switch (dif)
{
case 1:
if (dif == 1)
System.out.println ("Random number between 1 and 10 selected." + easy);
else if (dif == 3)
System.out.println ("Random number between 1 and 10000 selected.");
break;
}
while (guess != med)
{
System.out.print ("|" + med + "|" + "Random number between 1 and 1000 selected.");
guess = input.nextInt();
counter = counter + 1;
if (guess == med)
System.out.println ("YOU WIN MOFO!");
else if (guess < med)
System.out.println ("You're to cold!");
else if (guess > med)
System.out.println ("You're to hot!");
}
System.out.println ("It took you " + counter + " guess(es) to get it correct");
System.out.print ("Do you want to play again? (y/n): ");
input1 = input.nextLine(); // absorb enter key from integer
input1 = input.nextLine();
if (input1.equals("y"))
play = true;
else
play = false;
}
}
}

Do not combine a switch and an if for the same purpose (and put them inside each other). Pick one. Both are fine here.
In your construction here:
switch (dif)
{
case 1:
if (dif == 3) ...
}
The if can never be true, since you put it in the case 1 block. Obviously, dif can't be both 1 and 3 at the same time.

Related

do while loop syntax error, program not going through all of code, code efficiecy [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I made a basic number guessing game, everything was working fine until i tried to add a "play again" feature at the end of it. When the program is run, after inputting the first guess, it just starts the loop over again without going through the rest of it. Also, I am unsure if my code is efficient or not. It seems like too much coding for a simple concept. Is this an average length for a basic guessing program? Sorry if my questions are worded strangely. I'm a first year college student just learning the basics of programming. Here is my code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random randomNum = new Random();
boolean playing = true;
do {
int max = 100;
int min = 1;
int counter = 10;
int guess = 0;
int guessThis = min + randomNum.nextInt(max);
System.out.println("I'm thinking of a number between 1 and 100. You have 10 tries to guess it. What's your first guess?");
guess = input.nextInt();
counter--;
if (guess == guessThis) {
playing = false;
} else {
playing = true;
}
if (guess > max) {
System.out.println("I said the number is between 1 and 100. You think this is a GAME MUTHA FUCKA??! Guess again... :) " + counter + " guesses left.");
}
if (min > guess) {
System.out.println("Bruh. Are you stupid? " + guess + " is not between 1 and 100. Try again dummy boi. " + counter + " guesses left.");
}
if (guess > guessThis && min <= guess && guess >= max && playing == true && counter > 0) {
System.out.println("Too high. Guess again. " + counter + " guesses left.");
} else if (guess < guessThis && min <= guess && guess >= max && playing == true && counter >0) {
System.out.println("Too low. Guess again. " + counter + " guesses left.");
}
if (playing == false && counter > 0) {
System.out.println("You guessed it!");
}
if (counter <= 0) {
System.out.println("You lose! Ha! Fuck off broooooo. My number was " + guessThis);
playing = false;
}
}while (playing == true);
String answer;
if (playing == false) {
System.out.println("Wanna play again? (y/n)");
}
answer = input.next();
if (answer == "n") {
System.out.println("My game isn't fun enough for you? Wow, okay, rude. Bye then. Dh.");
input.close();
} if (answer == "y") {
playing = true;
}
}
}
use
}while (playing == true);
after the end of if statement
if (answer == "y") {
playing = true;
}

How can I complete my number guessing game?

My program should execute these steps:
Generate random no from 0 to 100.
Display random no and ask user enter (h/l/c)? (user have to enter one of them).
If it is correct ask user if they like to play again (y/n)? (user must answer (y/n))
I was able to execute Question no.1. Question no.2, random no display but I am unable to type character (h/l/c). Also, I am not able to ask player if they want to play again or not?
Here is what I have done:
import java.util.Random;
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int number;
int guess = 0;
int min = 0;
int max = 100;
int answer= (min+max);
number = (int) (Math.random() *100 +1);
System.out.println("Guess a number between 1 and 100.");
//Ask user to type higher 'h', lower 'l' and correct 'c'.
System.out.println("Is it " + number + " ?" + " (h/l/c): " );
//
guess = 50;
while (guess <= 7)
{
System.out.println( "It is: " + guess + "?" + "(h/l/c) : " );
//Type 'h' if guess is high, 'l' for low and 'c' for correct.
if(answer == 'h')
{
max = guess -1;
min = 0;
guess = ((max = min)/2) + min;
guess++;
} else if (answer == 'l')
{
max = 100;
min = guess + 1;
guess = ((max+min)/2);
guess++;
} else if (answer == 'c');
}
System.out.println("Great! Do you want to play again? (y/n): ");
if(answer == 'y')
{
System.out.println("Guess a number between 1 and 100.");
//else prompt another question with if else
} else{
System.exit(0);
}
}
}
Your program does not accept any user input about if the guess is too low or too high.
You declared "answer" with an equation so it should accept a number.
answer would require a char or String value.
answer = keyboard.nextChar(); //read a char
answer = keyboard.nextLine(); //read a String

Have trouble with a while loop and trying to terminate the program by a press of a letter

I have a problem with my program.
Program is a HILO game which requires the user to guess a number between a range of a generated number.
The problem is that once i press 0 on my keyboard, it reveals the random number, and essentially must end the loop and ask whether the user wants to continue or end the program. Instead, it continues the loop, and whenever I press a letter on the keyboard it shows me an Exception error which describes and input Miss Match. Can some one guide me on how to fix it?, maybe I wrote the program wrong, I tried multiple ways, it still doesn't work as it supposed to work.
Thank you.
import java.util.Scanner;
import java.util.Random;
import java.lang.Math;
public class HILOGAME
{
public static void firstGame()
{
final int range = 50;
int answer = 0;
int guesses;
int number;
int guessNum = 0;
String choice = "";
boolean Loop = false;
Scanner input = new Scanner(System.in);
Random r = new Random();
answer = r.nextInt(range) + 1;
System.out.println("IF YOU WISH TO GIVE UP, PRESS 0 ON THE KEYBOARD");
while (!Loop)
{
for (int exit = 0; exit < 10; exit++)
{
System.out.print("Guess a number between 1 and " + range + " : ");
number = input.nextInt();
guessNum = guessNum++;
guessNum += 1;
if (number == answer)
{
System.out.println(" Your guess was correct ");
System.out.println();
System.out.println("The number was: " + answer);
System.out.println("You guess the number with: " + guessNum + " guesses ");
System.out.println();
System.out.println("Enter x to continue to play or y to endgame");
choice = input.nextLine();
if (!choice.equalsIgnoreCase("x"))
{
Loop = true;
answer = r.nextInt(range) + 1;
System.out.print("A new number");
break;
}
else if (choice.equalsIgnoreCase("y"))
{
Loop = false;
System.out.print("END OF GAME");
exit = 11;
}
}
if (number > answer)
{
System.out.println("TOO HIGH");
}
if (number < answer)
{
System.out.println("TOO LOW");
}
if (number == 0)
{
System.out.println("YOU GAVE UP. THE NUMBER WAS " + answer);
System.out.println("Enter x to continue to play or y to endgame");
}
}
}
}
}
EDIT 1:
This code is working perfectly for me i have tried running it:
final int range = 50;
int answer = 0;
int guesses;
int number;
int guessNum = 0;
String choice = "";
boolean Loop = false;
Scanner input = new Scanner(System.in);
Random r = new Random();
answer = r.nextInt(range) + 1;
System.out.println("IF YOU WISH TO GIVE UP, PRESS 0 ON THE KEYBOARD");
while (!Loop)
{
int exit;
for (exit = 0; exit < 10; exit++)
{
System.out.print("Guess a number between 1 and " + range + " : ");
number = input.nextInt();
guessNum = guessNum++;
guessNum += 1;
if (number == answer)
{
System.out.println(" Your guess was correct ");
System.out.println();
System.out.println("The number was: " + answer);
System.out.println("You guess the number with: " + guessNum + " guesses ");
System.out.println();
System.out.println("Enter x to continue to play or y to endgame");
choice = input.next();
// HERE YOU ARE DOING THE WRONG THING
// What is the meaning of "not(!)" here ? !choice.equalsIgnoreCase("x")
// Remove "NOT(!)" from here your program will work as expected
if (choice.equalsIgnoreCase("x"))
{
//here you should assign loop to false if they continue
Loop = false;
answer = r.nextInt(range) + 1;
System.out.print("A new number");
break;
}
else if (choice.equalsIgnoreCase("y"))
{
//better to insert break here as well
//here you should assign loop to true if they wanna exit.
Loop = true;
System.out.print("END OF GAME");
exit = 11;
break;
}
}
else if (number > answer)
{
System.out.println("TOO HIGH");
}
else if (number < answer)
{
System.out.println("TOO LOW");
}
if (number == 0)
{
System.out.println("YOU GAVE UP. THE NUMBER WAS " + answer);
System.out.println("Enter x to continue to play or y to endgame");
// MADE CHANGES HERE
choice = input.next();
if (choice.equalsIgnoreCase("x"))
{
//here you should assign loop to false if they continue
Loop = false;
answer = r.nextInt(range) + 1;
System.out.print("A new number");
break;
}
else if (choice.equalsIgnoreCase("y"))
{
//better to insert break here as well
//here you should assign loop to true if they wanna exit.
Loop = true;
System.out.print("END OF GAME");
exit = 11;
break;
}
}
}
}
Please do copy-paste the code. Because it is working perfectly without any Exception..!!
My sample outputs:
SAMPLE 1:
Guess a number between 1 and 50 : 1
TOO LOW
Guess a number between 1 and 50 : 0
TOO LOW
YOU GAVE UP. THE NUMBER WAS 27
Enter x to continue to play or y to endgame
y
END OF GAME
SAMPLE 2:
Guess a number between 1 and 50 : 20
TOO HIGH
Guess a number between 1 and 50 : 19
TOO HIGH
Guess a number between 1 and 50 : 15
Your guess was correct
The number was: 15
You guess the number with: 3 guesses
Enter x to continue to play or y to endgame
x
A new numberGuess a number between 1 and 50 : 20
TOO LOW
Guess a number between 1 and 50 : 0
TOO LOW
YOU GAVE UP. THE NUMBER WAS 42
Enter x to continue to play or y to endgame
y
END OF GAME
Do let me know if there is any problem.
Well, if you wish to end a loop, you could use the break; command. It jumps out of a while or for loop. So you could use this if the number is 0:
if (number == 0)
{
System.out.println("YOU GAVE UP. THE NUMBER WAS " + answer);
System.out.println("Enter 'x' to continue to play or y to endgame");
choice = input.nextLine();
if(choice == "y")
{
break;
}
and add:
if(choice == "y") {break;} to the very end of the while(!Loop) loop.
And with the time when the answer is correct, you set Loop to false, which means !Loop would evaluate to true. You should change Loop = false to Loop = true when they decide to continue the game.
a small modification
public class HILOGAME {
public static void firstGame() {
final int range = 50;
int answer;
int number;
int guessNum = 0;
String choice = "";
Scanner input = new Scanner(System.in);
Random r = new Random();
answer = r.nextInt(range) + 1;
System.out.print("Guess a number between 1 and " + range + " : ");
number = input.nextInt();
if (number == answer) {
System.out.println(" Your guess was correct ");
System.out.println();
System.out.println("The number was: " + answer);
System.out.println("You guess the number with: " + guessNum + " guesses ");
System.out.println();
} else {
System.out.println("you entered a wrong value");
System.out.println("do you really want to try again? press y to continue or other alphabet to exit");
choice = input.next();
playAgain(choice);
}
}
public static void playAgain(String choice) {
if (choice.equalsIgnoreCase("Y")) {
firstGame();
} else {
System.out.println("you chose to quit");
System.exit(0);
}
}

How to make int counter go down when answer is wrong

Im making a player 2 guesses player 1's number game. Ive made an int counter thats == 10 and is meant to go down everytime player 2 gets answer wrong. I cant get it to work and i need help on how to make this. Youll see what i mean...
package guessMain;
import java.awt.*;
import java.util.Scanner;
public class GuessCodeSource {
public static void main(String[] args){
System.out.println("WELCOME TO GUESSING GAME BY JOSH!");
System.out.println("Rules: Player 1 picks number between 1 - 100 while Player 2 has 10 tries to guess");
Scanner josh = new Scanner(System.in);
System.out.println("Enter name here PLAYER 1: ");
String p1 = josh.nextLine();
System.out.println("Enter name here PLAYER 2: ");
String p2 = josh.nextLine();
System.out.println("Ok, " + p2 + " look away. " + p1 + ", Please enter a number and press enter:");
int answer = josh.nextInt();
if (answer >= 100){
System.out.println("BUSTED! I said a number between 1 - 100!");
}else if (answer <= 100){
System.out.println("Guess in the space below.");
int guess = josh.nextInt();
if (guess == answer){
System.out.println("CORRECT!!!!!");
}else if (guess != answer);
for (int counter = 10; counter-=1);
System.out.println("You have " + count + " of guesses left");
}
}
}
To make reduce a number by one, use the decrement operator.
For example,
counter--;
would subtract one from the counter.
If you want to subtract more than one, you can use the "-=" operator in the following manner:
counter -= 2;
So, in your code, in the final else if block, you could change the code to the following to reduce "counter" by 1.
else if (guess != answer) {
counter--;
System.out.println("You have " + count + " of guesses left");
}
But, in your code, you never declare the variable counter. Somewhere, most likely at the top of your code, you want to create this variable. To create an Integer variable you do the following:
int counter = 10;
You asked how to LOOP as well, so here it is. Read the comments to gain understanding of what the code does. If you have more questions, ask below.
public static void main(String[] args) {
System.out.println("WELCOME TO GUESSING GAME BY JOSH!");
System.out.println("Rules: Player 1 picks number between 1 - 100 while Player 2 has 10 tries to guess");
Scanner josh = new Scanner(System.in);
int guess = 0; // Create these variables up here to access them everywhere in "main"
int counter = 0;
boolean continueTheGame = true; // A boolean variable that holds ONLY either true or false
System.out.println("Enter name here PLAYER 1: ");
String p1 = josh.nextLine();
System.out.println("Enter name here PLAYER 2: ");
String p2 = josh.nextLine();
System.out.println("Ok, " + p2 + " look away. " + p1 + ", Please enter a number and press enter:");
int answer = josh.nextInt();
// A while loop will continue as long as a boolean expression is true.
// So, we create a boolean variable somewhere above called "continueTheGame"
// As long as this is true, the code INSIDE of the while loop's brackets will repeat.
// If the user has less than zero guesses left, we can set the variable to false,
// which will make the loop stop!
while (continueTheGame == true) { // The start of the while loop
if (answer >= 100) {
System.out.println("BUSTED! I said a number between 1 - 100!");
} else if (answer <= 100) {
System.out.println("Guess in the space below.");
guess = josh.nextInt();
}
if (guess == answer) {
System.out.println("CORRECT!!!!!");
} else if (guess != answer) {
counter--;
System.out.println("You have " + counter + " of guesses left");
if (counter > 0) { // If they have MORE than zero guesses left, loop again!
continueTheGame = true;
} else { // If they have zero guesses left, make it stop looping
continueTheGame = false;
}
}
}
// Once the loop ends, the code will start again here,
// because the bracket above is the final bracket of the WHILE loop
}
Okay, so here is the full functional main method you are looking for:
public static void main(String[] args){
System.out.println("WELCOME TO GUESSING GAME BY JOSH!");
System.out.println("Rules: Player 1 picks number between 1 - 100 while Player 2 has 10 tries to guess");
Scanner josh = new Scanner(System.in);
System.out.println("Enter name here PLAYER 1: ");
String p1 = josh.nextLine();
System.out.println("Enter name here PLAYER 2: ");
String p2 = josh.nextLine();
System.out.println("Ok, " + p2 + " look away. " + p1 + ", Please enter a number and press enter:");
int answer = josh.nextInt();
if (answer >= 100){
System.out.println("BUSTED! I said a number between 1 - 100!");
}else {
System.out.println("Guess in the space below.");
}
for (int count = 10; count>=0; count--) {
int guess = josh.nextInt();
if (guess == answer){
System.out.println("CORRECT!!!!!");
System.exit(0);
} else {
System.out.println("You have " + count + " of guesses left");
if (count == 0) {
System.out.println("Sorry, you lost, no more tries..");
System.exit(0);
}
}
}
josh.close();
}

How to limit the user can only play the guessing game only for 5 times but it is not the user attempts on guessing in Java? [duplicate]

This question already has answers here:
converting while loop to a for each loop
(2 answers)
Closed 8 years ago.
Here is my code:
package playthegame;
import java.util.*;
public class PlayTheGame {
public static void main(String[] args) {
Random randomNumber = new Random();
int numberToGuess;
int numberOfTries;
int numberOfLimit = 5;
int userInput;
ArrayList<Integer> theInputs = new ArrayList();
Scanner in = new Scanner(System.in);
int start = 0;
boolean winTracker = false;
int temp;
while (0 == start) {
numberToGuess = randomNumber.nextInt(10);
userInput = 0;
numberOfTries = 0;
System.out.println("Please guess the number between 1 and 10: ");
while (numberToGuess != userInput && numberOfTries < numberOfLimit) {
userInput = in.nextInt();
temp = userInput;
numberOfTries++;
if (userInput == numberToGuess) {
theInputs.add(userInput);
System.out.println("You win!\n");
System.out.println("The guess number is " + numberToGuess+"\n");
System.out.println("It takes a total of " + numberOfTries + " guesses\n");
System.out.println("These are the guesses number : " + theInputs+"\n");
theInputs.clear();
} else if (userInput < numberToGuess) {
theInputs.add(userInput);
System.out.println("Oppss, The number is too small\n");
System.out.println("Try next guess: \n");
} else if (userInput > numberToGuess) {
theInputs.add(userInput);
System.out.println("Oppss, The number is too High\n");
System.out.println("Try next guess: \n");
}
}
if (numberToGuess != userInput) {
System.out.println("==============================================================");
System.out.println("Oppss..You are out of Guess Limitation!The correct number is: " + numberToGuess);
}
}
}
}
How to limit the user can only play the guessing game only for 5 times in Java if the user whether have won or lost playing the game?
is there any possibility to set the user only play for 5 times?anyone, please help
You are in an endless loop with the
while(0 == start)
statement because the start variable is never reassigned to another value after assigning zero to it. You can just remove that loop or add a
start = 1;
statement once the user has 5 tries.

Categories

Resources