I made a java program named “Guess the number” in which the computer will generate a random number and we will have to give a number in the input. The computer will check it and tell us the number we guessed by giving in the input.I created a java program named “Guess the number” in which the computer will generate a random number and we have to give a number as input. The computer will check and tell whether the number we guessed by giving it in the input is correct and matches the generated number or not.
If our guessed number is more than the generated number then If our guessed number is more than the generated number then we will see “Too High…” in the output and if it is smaller then “Too Low…” will be shown.
I am getting an error in the program I made that whatever number I give in the input, it always appears as “Too High…”. I also looked at its solution and my code was not correct.I am getting an error in the program I made that whatever number I give in the input it always comes out as “Too High…”. I also looked at its solution, my code is correct, only I have not called the method which says No Of Guess or Attempts.
My Code:
import java.util.Random;
import java.util.Scanner;
class Game{
public int number;
public int inputNumber;
public int noOfGuess = 0;
public void setNoOfGuess(int noOfGuess){
this.noOfGuess = noOfGuess;
}
public int getNoOfGuess(){
return noOfGuess;
}
Game(){
Random rand = new Random(100);
this.number = rand.nextInt();
}
void takeUserInput(){
System.out.println("Guess the Number:");
Scanner sc = new Scanner(System.in);
inputNumber = sc.nextInt();
}
boolean isCorrectNumber(){
if(inputNumber==number){
System.out.println("You've Guessed the Correct Number");
return true;
}
else if(inputNumber<number){
System.out.println("Too Low...");
}
else if(inputNumber>number){
System.out.println("Too High...");
}
return false;
}
}
public class Main
{
public static void main(String[] args) {
Game g = new Game();
boolean b = false;
while(!b){
g.takeUserInput();
b = g.isCorrectNumber();
}
}
}
Solution:
package com.company;
import java.util.Random;
import java.util.Scanner;
class Game{
public int number;
public int inputNumber;
public int noOfGuesses = 0;
public int getNoOfGuesses() {
return noOfGuesses;
}
public void setNoOfGuesses(int noOfGuesses) {
this.noOfGuesses = noOfGuesses;
}
Game(){
Random rand = new Random();
this.number = rand.nextInt(100);
}
void takeUserInput(){
System.out.println("Guess the number");
Scanner sc = new Scanner(System.in);
inputNumber = sc.nextInt();
}
boolean isCorrectNumber(){
noOfGuesses++;
if (inputNumber==number){
System.out.format("Yes you guessed it right, it was %dnYou guessed it in %d attempts", number, noOfGuesses);
return true;
}
else if(inputNumber<number){
System.out.println("Too low...");
}
else if(inputNumber>number){
System.out.println("Too high...");
}
return false;
}
}
public class Main{
public static void main(String[] args) {
/*
Create a class Game, which allows a user to play "Guess the Number"
game once. Game should have the following methods:
1. Constructor to generate the random number
2. takeUserInput() to take a user input of number
3. isCorrectNumber() to detect whether the number entered by the user is true
4. getter and setter for noOfGuesses
Use properties such as noOfGuesses(int), etc to get this task done!
*/
Game g = new Game();
boolean b= false;
while(!b){
g.takeUserInput();
b = g.isCorrectNumber();
}
}
}
Ankur Jha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.