Right now im making a HiLo game in java and i have a few problems, not sure tho if im doing it right since im very new into java.
I having problem with getting the two ints "answer" and "guess" to the 3rd method.
and i must use 3 methods in this task.
Method 1: asking for what lvl the user wants.
Method 2: The program picks a random number between max and lets the user guess.
Method 3: Will see if the guess is == to answer and if not, it lets the user to try again. and at the same time i have to save all the tries from the user.
import java.util.Scanner;
import java.util.Random;
public class oneagain {
public static void main(String[] args) {
System.out.println("Welcome HiLo!");
System.out.println("What lvl?");
System.out.println("1. Easy (1-10)");
System.out.println("2. Medium (1-100)");
System.out.println("3. Hard (1-1000)");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
int tries = (playGame(choice));
}
public static int playGame(int choice) {
Scanner sc = new Scanner(System.in);
int tries = 0;
if (choice == 1) {
System.out.println("Guess a number between 1 and 10");
int answer = (int) (Math.random() * 11) + 1;
int guess = sc.nextInt();
}
if (choice == 2) {
System.out.println("Guess a number between 1 and 100");
int answer = (int) (Math.random() * 101) + 1;
int guess = sc.nextInt();
}
if (choice == 3) {
System.out.println("Guess a number between 1 and 1000");
int answer = (int) (Math.random() * 1001) + 1;
int guess = sc.nextInt();
}
return tries;
}
public static void giveResponse(int answer, int guess) {
if (choice == 1) {
int tries = 0;
Scanner sc = new Scanner(System.in);
boolean y = false;
while (y == false) {
guess = sc.nextInt();
tries++;
if (guess == answer) {
y = true;
} else if (guess < answer) {
System.out.println("Guess was too low!");
System.out.println("Guess a number between 1 and 10:");
} else if (guess > answer) {
System.out.println("Guess was too high!");
System.out.println("Guess a number between 1 and 10:");
} else if (guess == answer) {
System.out.println("Score!");
System.out.println("You succeeded in " + tries + " attempt");
}
if (choice == 2) {
while (y == false) {
guess = sc.nextInt();
tries++;
if (guess == answer) {
y = true;
} else if (guess < answer) {
System.out.println("Guess was too low!");
System.out.println("Guess a number between 1 and 100:");
} else if (guess > answer) {
System.out.println("Guess was too high!");
System.out.println("Guess a number between 1 and 100:");
} else if (guess == answer) {
System.out.println("Score!");
System.out.println("You succeeded in " + tries + " attempt");
}
if (choice == 3) {
while (y == false) {
guess = sc.nextInt();
tries++;
if (guess == answer) {
y = true;
} else if (guess < answer) {
System.out.println("Guess was too low!");
System.out.println("Guess a number between 1 and 100:");
} else if (guess > answer) {
System.out.println("Guess was too high!");
System.out.println("Guess a number between 1 and 100:");
} else if (guess == answer) {
System.out.println("Score!");
System.out.println("You succeeded in " + tries + " attempt");
}
}
}
}
}
}
}
}
}
Related
I have written the code below. I have run the program and it allows the user to guess the correct number and return the message successfully. However, I couldn't get it to regenerate a new random number? I also couldn't include an option to ask whether the user wants to quit or not. Please help. Thank you.
import java.util.*;
class CompterAge {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
boolean correctGuess = false;
Random r = new Random();
int randNum = r.nextInt(100-1+1) + 1;
while (!correctGuess) {
System.out.println("Guess the computer's age: ");
int guess = sc.nextInt();
if ((guess > 0) && (guess <= 100)) {
if (guess > randNum) {
System.out.println("Your guess is bigger than the number. You should go lower.");
correctGuess = false;
} else if (guess < randNum) {
System.out.println("Your guess is smaller than the number. You should go higher.");
correctGuess = false;
} else {
System.out.println("Congratulations. You got the number! The number is " + randNum);
System.out.println("Do you wish to continue the game? Yes/No");
String input = sc.next();
if (input == "Yes") {
correctGuess = false;
} else {
break;
}
}
} else {
System.out.println("Please enter a number between 1 to 100.");
correctGuess = false;
}
}
}
}
This code works, I have added another flag variable and corrected some logical errors. See the comments.
boolean correctGuess = false;
boolean endGame = false;
Random r = new Random();
while (!endGame){
int randNum = r.nextInt(101); //Why did you have (100-1+1) + 1 ?? Simple (101) was enough
correctGuess = false;
while (!correctGuess) {
System.out.println("Guess the computer's age: ");
int guess = sc.nextInt();
if ((guess > 0) && (guess <= 100)) {
if (guess > randNum) {
System.out.println("Your guess is bigger than the number. You should go lower.");
correctGuess = false;
} else if (guess < randNum) {
System.out.println("Your guess is smaller than the number. You should go higher.");
correctGuess = false;
} else {
correctGuess = true; //Will exit the Inner Loop
System.out.println("Congratulations. You got the number! The number is " + randNum);
System.out.println("Do you wish to continue the game? Yes/No");
String input = sc.next().toLowerCase();
if (input.equals("yes")) { //You can not use == for String Comparisons
endGame = false;
} else {
endGame = true;
}
}
} else {
System.out.println("Please enter a number between 1 to 100.");
correctGuess = false;
}
}
}
Your random number generation is done outside of your while loop so when they input that they want to continue the game it should then generate a new number:
System.out.println("Congratulations. You got the number! The number is " + randNum);
System.out.println("Do you wish to continue the game? Yes/No");
String input = sc.next();
if (input.equals("Yes")) {
randNum = r.nextInt(100-1+1) + 1;
correctGuess = false;
} else {
break;
}
I'm a beginner making a guessing game and I am trying to let the player quit the game by making their guess -1. Currently, if I enter -1 it says Too low and asks me to keep guessing and the player cannot quit the game until they guess the number.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char choice;
do {
int randomNum = (int) (Math.random() * 100 + 1);
int guess = 0;
while (guess != randomNum) {
System.out.println("Guess a number between 1-100");
guess = scan.nextInt();
if (guess > randoomNum) {
System.out.println("Too high.");
}
else if (guess < randoomNum) {
System.out.println("Too low.");
}
else if (guess > 0) {
System.exit(0);
System.out.println("GAME OVER");
System.out.println("the number was " + randomNum + ".");
}
else {
System.out.println("Correct! Well done!");
}
}
System.out.println("\nPlay again? (Y/N)");
choice = scan.next() .charAt(0);
} while (choice == 'Y' | choice =='y');
System.out.println("GAME OVER");
}
The existing code needs to be modified slightly: the condition with exit should be checked before comparing to the randomNum.
Other issues to be addressed:
Use input for Scanner instance
Perform System.exit after printing goodbye message
Scanner input = new Scanner(System.in);
char choice;
do {
int randomNum = (int) (Math.random() * 100 + 1);
int guess = 0;
while (guess != randomNum) {
System.out.println("Guess a number between 1-100, or -1 to exit");
guess = input.nextInt();
if (guess == -1) {
System.out.println("GAME OVER");
System.out.println("the number was " + randomNum + ".");
System.exit(0);
}
if (guess > randomNum) {
System.out.println("Too high.");
}
else if (guess < randomNum) {
System.out.println("Too low.");
}
else {
System.out.println("Correct! Well done!");
}
}
System.out.println("\nPlay again? (Y/N)");
choice = input.next().charAt(0);
} while (choice == 'Y' || choice =='y');
System.out.println("GAME OVER");
For my Java class, I'm supposed to make a random number guessing game. I've been stuck on a loop I created for the past couple of days. The output of the program is always an infinite loop and I can't see why. Any help is very much appreciated.
/*
This program will generate a random number.
It will ask the user to guess what number was generated and say
if the guess is too high or low.
*/
import java.util.Scanner;
import java.util.Random;
public class RandomNumGame {
public static void main(String[] args) {
Random rand = new Random();
Scanner input = new Scanner(System.in);
int randNum = rand.nextInt(20);
System.out.println("Number is : " + randNum);
int userGuess = 0;
int success = 0;
System.out.println("Guess the number: ");
userGuess = input.nextInt();
while(success == 0)
{
if(userGuess > randNum){
System.out.println("Too high");
}
else if(userGuess < randNum){
System.out.println("Too high");
}
else{
System.out.println("Something is very wrong.");
}
}
if(userGuess == randNum){
success++;
System.out.println("You got it! Play again?");
}
}
}
You put the if that checks if the input is equal to the number outside the while, so the cycle never ends.
Here is the code fixed:
import java.util.Scanner;
import java.util.Random;
public class RandomNumGame {
public static void main(String[] args) {
Random rand = new Random();
Scanner input = new Scanner(System.in);
int randNum = rand.nextInt(20);
System.out.println("Number is : " + randNum);
int userGuess = 0;
int success = 0;
System.out.println("Guess the number: ");
userGuess = input.nextInt();
while(success == 0) {
if(userGuess > randNum) {
System.out.println("Too high");
} else if(userGuess < randNum) {
System.out.println("Too high");
} else if(userGuess == randNum) {
success++;
System.out.println("You got it! Play again?");
} else {
System.out.println("Something is very wrong.");
}
}
}
}
I fixed it! I added breaks after each fail condition so that I wouldn't get an infinite loop. I tried this earlier, but I kept getting errors.
This is the completed code:
import java.util.Scanner;
import java.util.Random;
public class RandomNumGame {
public static void main(String[] args) {
Random rand = new Random();
Scanner input = new Scanner(System.in);
int randNum = rand.nextInt(20);
System.out.println("Number is : " + randNum);
int userGuess = 0;
boolean success = false;
System.out.println("Guess the number: ");
userGuess = input.nextInt();
input.close();
while(success == false){
if(userGuess > randNum) {
System.out.println("Too high,try again");
break;
} else if(userGuess < randNum) {
System.out.println("Too low");
break;
} else if(userGuess == randNum) {
success = true;
System.out.println("You got it! Play again?");
} else {
System.out.println("Something is very wrong.");
}
}
}
}
I am doing a guessing game where in I can input 1-100 but I am having a trouble in only accepting numbers if I typed a letter when I first run the program it will give me error and execute the program instantly image herebut if ityped number after I start the program and type letter next it give me a wrong message it should only display message saying "invalid input".image here Any suggestion thanks.
package m1;
import java.util.InputMismatchException;
import java.util.Scanner;
public class M1{
public static void main(String[] args) {
Scanner Scanner = new Scanner(System.in);
int between = 100;
int secretNumber = (int)(Math.random()*between);
int inputNum = 0;
int guesses = 0;
System.out.println("Please enter your guess: ");
inputNum = Scanner.nextInt();
guesses++;
while (inputNum != secretNumber) {
try {
// number too high or too low
if (inputNum > 100 || inputNum < 1) {
System.out.println("Out of Range!");
System.out.println("Enter a guess between 1 and " + between + ".");
inputNum = Scanner.nextInt();
}
// less than secretNumber
if (inputNum < secretNumber) {
System.out.println("Too Low...Try Again!");
inputNum = Scanner.nextInt();
guesses++;
}
// greater than secretNumber
if (inputNum > secretNumber) {
System.out.println("Too High...Try Again!");
inputNum = Scanner.nextInt();
guesses++;
}
}
catch(InputMismatchException e){
System.out.println("Invalid Input");
Scanner.next();
}
}
System.out.println("\nWell done! The secret number was " + secretNumber + "." + "\nYou took " + guesses + " guesses.");
}
}
Generally, name variable names in java using camelCase in most cases.
You don't actually need to catch any exception in your case as you can simply do scanner.next() if scanner.hasNextInt() is false. Prompting the user to enter specifically a number this time.
Try the below code:
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
class Main {
private static final String GUESS_PROMPT_PATTERN = "Please enter a guess between %d and %d inclusive: ";
private static final String WIN_PROMPT_PATTERN = "Well done! The secret number was %d. You took %d guesses.\n";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int minimumGuess = 1, maximumGuess = 100;
int secretNumber = ThreadLocalRandom.current().nextInt(minimumGuess, maximumGuess + 1);
int guesses = 0;
String guessPrompt = String.format(GUESS_PROMPT_PATTERN, minimumGuess, maximumGuess);
System.out.println("Lec's Guessing Game");
System.out.println("====================");
System.out.print(guessPrompt);
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
guesses++;
int inputNum = scanner.nextInt();
if (inputNum == secretNumber) {
break;
}
// Input number too high or too low.
if (inputNum > maximumGuess || inputNum < minimumGuess) {
System.out.println("Out of Range!");
scanner.nextLine();
System.out.print(guessPrompt);
}
// Input number was less than the secret number.
else if (inputNum < secretNumber) {
System.out.println("Too Low... Try Again!");
System.out.print(guessPrompt);
}
// Input number was greater than the secret number.
else {
System.out.println("Too High... Try Again!");
System.out.print(guessPrompt);
}
} else {
System.out.print("ERROR: Invalid Input");
System.out.print("Please enter a number: ");
scanner.next();
}
}
System.out.printf(WIN_PROMPT_PATTERN, secretNumber, guesses);
}
}
How would I limit the tries of a simple game to just three? I would think you would use a boolean. But not sure.
import java.util.Scanner;
public class guess {
public static void main(String[] args) {
int randomN = (int) (Math.random() * 10) + 1;
Scanner input = new Scanner(System.in);
int guess;
System.out.println("Enter a number between 1 and 10.");
System.out.println();
do {
System.out.print("Enter your guess: ");
guess = input.nextInt();
if (guess == randomN) {
System.out.println("You won!");
} else if (guess > randomN) {
System.out.println("Too high");
} else if (guess < randomN) {
System.out.println("Too low");
}
} while (guess != randomN);
}
}
int attempts = 0;
do{
attempts++;
....
}while(guess != randomN && attempts < 3);
Use a flag. Initialize it as 0. If guess is correct then reset it as 0. If not increase by 1. Before each guess, check if flag > 2. If no let continue, if yes break.
You can increment during the failure of a guess. I believe the variable should be located outside of the loop. Then what's left is to add a portion that notifies the user of a failure when guesses run out.
public static void main(String[]args) {
int rNumber = (int)(Math.random() * 10) + 1;
Scanner input = new Scanner(System.in);
int guess;
int tries = 0;
int success = 0;
System.out.println("Enter a number between 1 and 10.");
System.out.println();
do {
System.out.println("Enter your guess: ");
guess = input.nextInt();
if(guess == rNumber) {
System.out.println("You guessed right! You win!");
success++;
} else if (guess < rNumber) {
System.out.println("Too low");
tries++;
} else if (guess > rNumber) {
System.out.println("Too high.");
tries++;
}
} while(tries != 3 && success != 1 || success != 1);
}