Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:35:18+00:00 2026-06-01T06:35:18+00:00

I am trying to code a number guessing class and client. The issue/problems I

  • 0

I am trying to code a number guessing class and client.

The issue/problems I am having with this class/client is that my number guess either ends up too high or too low and in top of that it loops the number twice when it should once.

Number generated from 0 to 100. Wanna take a guess(enter 0 to give up)
Let's take a guess: 
50
40
Your guess is too low

What possible change can I make to improve the overall loop or change.

Here is my code for anyone that wants to look at it.

import java.util.Random;

public class NumberGuess
{

  private Random generator;
  private int Number;


  int intGuess= (1 + (int)(Math.random()*100));
  int numGuess=0;
  boolean isGuessCorrect=false;

  public NumberGuess(){
  }

  int numguess;
  public int guess(int guessIn){
    int numguess=guessIn;
    if(numguess>intGuess){
      return 1;
    }else if(isGuessCorrect){
      return 0;
    }else{
      return -1;
    }
  }

  public int getNumberofGuesses(){
    return numGuess;
  }

  public boolean gameIsComplete(){
    if(isGuessCorrect){
      return true;
    }else{
      return false;
    }
  }

  public void reset(){
    intGuess=(1 + (int)(Math.random()*100));
    numGuess=0;
    isGuessCorrect=false;
  }
}

Client class

import java.util.Scanner;

public class NumberGuessclient{
    public static void main(String[] args){

      NumberGuess game1=new NumberGuess();
      Scanner scan = new Scanner(System.in);            

      int quit=1;
      while(quit != 0) {
        System.out.println("Number generated from 0 to 100. Wanna take a guess(enter 0 to give up)");
        System.out.println("Let's take a guess: ");

        int guess1= scan.nextInt(); 

    while((guess1 != 0)||(!game1.gameIsComplete())) {
            guess1 = scan.nextInt();
            if (game1.guess(guess1)==1){
              System.out.println("Your guess is too high");
            }
            else if(game1.guess(guess1)==-1) {
                      System.out.println("Your guess is too low");
                }
                else {                      System.out.println("guessed in " + game1.getNumberofGuesses() + " tries");
                }
            }
            System.out.println("Enter 1 for new game, 0 to quit: ");
            quit = scan.nextInt();
            if(quit==1){
                game1.reset();
            }
        }
    }
}

source code for more ELABORATION if not clarified above.

NumberGuess Class:

The NumberGuess class will facilitate a number guessing game. The constructor should generate a random number, saving the number in a private class field. The class should also define a method which accepts a “guess”, compares the “guess” to the randomly generated number, and returns one of the following:

•   -1 the guess was less than the secret number
•    0 the guess matched the secret number
•    1 the guess was higher than the secret number 

Determine whether other methods, constructors or otherwise, would be useful for this class.

The Java API defines a Random class for generating random numbers. The class can be reviewed in the API or in your textbook beginning on page 250. Consider limiting the range of the random number. For instance, a number between 0 and 100.

Client Application:

The client application allows the end-user to play the number guessing game. Below is a sample run. Your application does not need to match.
I’m thinking of a number between 0 and 100. Can you guess it?

Take a guess: 50
Your guess is too high
Another guess? (Y or N): y
Take a guess: 25
Your guess is too high
Another guess? (Y or N): y
Take a guess: 10
Your guess is too low
Another guess? (Y or N): y
Take a guess: 15
Your guess is too low
Another guess? (Y or N): y
Take a guess: 18
Your guess is too low
Another guess? (Y or N): y
Take a guess: 20
Congratulations! You correctly guessed the secret number in 6 tries.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:35:19+00:00Added an answer on June 1, 2026 at 6:35 am

    There are quite a bit of things that you can change, for instance:

    • There’s no need to call game1.guess(guess1) more than once every loop.
    • The completion method is quite long…
    • You should call scan.nextInt() before entering the while loop…
    • I’m assumming the last output was manually generated, because you never increment numguess
    • The guess(int) method doesn’t work…

    I’m not usually one to do homework… but (I’m having a good day…!):

    import java.util.Random;
    import java.util.Scanner;
    
    public class NumberGuessclient {
    
        private static final String[] ANS = {
            "Your guess is too low\n", 
            "guessed in %d tries\n", 
            "Your guess is too high\n"
        };
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
    
            while (true) {
                NumberGuess game = new NumberGuess();
                System.out.println("Number generated from 0 to 100. Wanna take a guess(enter 0 to give up)");
                System.out.println("Let's take a guess: ");
    
                while (!game.isGameComplete()) {
                    System.out.format(ANS[game.guess(scan.nextInt())+1], game.getNumberofGuesses());
                }
    
                System.out.println("Enter 1 for new game, 0 to quit: ");
    
                if (scan.nextInt() != 1) {
                    System.out.println("Bye!");
                    System.exit(0);
                }
            }
        }
    }
    
    class NumberGuess {
        private static final Random RAND_GENERATOR = new Random(System.nanoTime());
        int intGuess = RAND_GENERATOR.nextInt(101);
        int numGuess = 0;
        boolean isGuessCorrect = false;
    
        public int guess(int guessIn) {
            numGuess++;
            if (guessIn > intGuess) {
                return 1;
            } else if (guessIn == intGuess) {
                isGuessCorrect = true;
                return 0;
            } else {
                return -1;
            }
        }
    
        public int getNumberofGuesses() {
            return numGuess;
        }
    
        public boolean isGameComplete() {
            return isGuessCorrect;
        }
    }
    

    Now one comment: as I recall the “fun” of this game was that you should always “guess” the number in at the very most 10 tries, you could implement that…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having an issue converting type. I was trying code like this (minimal,
I'm trying to design a code where one guess a number. I defined the
I'm trying to write a very simple number guessing game (code is below). After
I am trying this code but i want to know what is the number
I am trying to code a random number generation function in embedded C where
I'm trying to keep my code clean and keep the number of files down.
I'm trying to get this C++ code to input a series of numbers from
I am trying to write code for phone number validation. I need to just
I'm trying to code a method which loads a map according from a number
I'm trying to code a loan calculator. I seem to be having issues. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.