I am a beginner and as you can see I made a simple java game where the user has 5 tries to guess a number between 1 and 20 and if he won a congratulations message will show, and if he didn't succeed a game over message will pop up
but the problem is when you get the right answer on the 5th try both congratulations and game over messages will pop up how can I fix it
package org.meicode.Loops;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Welcome");
System.out.println("Enter your name please ");
Scanner scanner =new Scanner(System.in);
String name= scanner.next();
System.out.println("Hello " name);
System.out.println("Type 1 to start the game");
int yes = scanner.nextInt();
while (yes!=1) {
System.out.println("Type 1 to start the game");
yes = scanner.nextInt();
}
System.out.println("Guess the number in my mind,It is between 1 and 20 and you got 5 tries");
int timestried = 0;
Random random = new Random();
int x = random.nextInt(20) 1;
while (timestried < 5 ) {
timestried ;
Scanner scanner1 = new Scanner(System.in);
int answer = scanner.nextInt();
if (x == answer) {
System.out.println("Well done, you did it");
} else if (x > answer) {
System.out.println("Try again,hint:the value is bigger than what you typed");
} else if(x< answer) {
System.out.println("Try again,hint:the value is smaller than what you typed");
}
}
System.out.println("Game over, the number was " x);
}
}
CodePudding user response:
Here is my attempt. I have added some comments in the code to help you.
Note that I have changed some of the file names to, so you may need to change them back for it to run, or just copy the main code section:
package com.misc;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;
public class GameTest {
public static void main(String[] args) {
System.out.println("Welcome");
System.out.println("Enter your name please ");
Scanner scanner =new Scanner(System.in);
String name= scanner.next();
System.out.println("Hello " name);
System.out.println("Type 1 to start the game");
int yes = scanner.nextInt();
//We first put answer into here so we can prevent the game over condition from appearing at the end using an if-statement that compares answer.
int answer=0;
while (yes!=1) {
System.out.println("Type 1 to start the game");
yes = scanner.nextInt();
}
System.out.println("Guess the number in my mind,It is between 1 and 20 and you got 5 tries");
int timestried = 0;
Random random = new Random();
int x = random.nextInt(20) 1;
//This line is just for testing purpose, so we can see that the game over no longer appears after guessing correctly on the fifth try.
System.out.println("Testing: the answer is " x);
while (timestried < 5 ) {
timestried ;
Scanner scanner1 = new Scanner(System.in);
answer = scanner.nextInt();
if (x == answer) {
System.out.println("Well done, you did it");
} else if (x > answer) {
System.out.println("Try again,hint:the value is bigger than what you typed");
} else if(x< answer) {
System.out.println("Try again,hint:the value is smaller than what you typed");
}
}
//We no longer print the game over if the answer is correct on the fifth try.
if(x!= answer) {
System.out.println("Game over, the number was " x);
}
}
}
Here is proof that it works. I made the program print out the real answer, answered wrong 4 times and correctly the 5th time.
CodePudding user response:
There are two things I would add to your code to achieve the desired behavior.
Firstly, you probably want to break the loop when the user types the correct answer:
if (x == answer) {
System.out.println("Well done, you did it");
break;
}
Secondly, you can add a flag that is set to true if the user types the correct answer:
boolean userHasAnsweredCorrect = false;
while (timesTried < 5) {
if (x == answer) {
System.out.println("Well done, you did it");
userHasAnsweredCorrect = true;
break;
}
}
// omitted some lines .. then at the end
if (userHasAnsweredCorrect) {
System.out.println("You beat the game!")
} else {
System.out.println("Game over, the number was " x);
}