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

  • Home
  • SEARCH
  • 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 8808899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:44:10+00:00 2026-06-14T02:44:10+00:00

Here’s my code: import java.util.Scanner; import static java.lang.System.*; public class GuessingGame { private int

  • 0

Here’s my code:

import java.util.Scanner;
import static java.lang.System.*;

public class GuessingGame
{
private int upperBound;
private int count, guess, num, pct;

public GuessingGame(int stop)
{
    upperBound = stop;
}

public void setNum(int stop)
{
    upperBound = stop;
}

public void playGame()
{
    int count = 0;
    int attempt = 1;
    Scanner keyboard = new Scanner(System.in);
    //upperBound = keyboard.nextInt();
     num = (int)Math.random()*upperBound;
     guess = 0;
     out.println("Enter a number between 1 and " + upperBound);
        guess = keyboard.nextInt();
        count++;
        if(guess != num)
            attempt++;
    do{
        out.println("Enter a number between 1 and " + upperBound);
        guess = keyboard.nextInt();
        count++;
        if(guess != num)
            attempt++;
    }while(guess != num);
     pct = (count/attempt)*100;



}

public String toString()
{
    String output="";
    output = "It took you " + count + " tries to guess " + num + "\n you guessed wrong " + pct + "% of the time";
    return output;
}
}

I know that it has to at some point guess has to be equal to num but the code never ends the current “game” but it seems to be infinitely looping when I use my example of 5 for the stop/upperBound

Here’s my runner class as requested:

import java.util.Scanner;
import static java.lang.System.*;

public class Lab10e
{
public static void main(String args[])
{
    Scanner keyboard = new Scanner(System.in);
    char response = ' ';

    out.print("Guessing Game - how many numbers? ");

    //read in the player value
    int stop = keyboard.nextInt();

    GuessingGame game = new GuessingGame(stop);
    game.playGame();
    out.println(game);
    out.println("would you like to play again? (y/n):: ");
    String resp =  keyboard.next();
    response = resp.charAt(0);

    do {
    out.print("Guessing Game - how many numbers? ");
    stop = keyboard.nextInt();
    game.setNum(stop);
    game.playGame();
    out.println(game);
    out.println();
    out.println("would you like to play again? (y/n):: ");
    resp =  keyboard.next();
    response = resp.charAt(0);
    //



}while(response == 'y'); 

}

}
  • 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-14T02:44:12+00:00Added an answer on June 14, 2026 at 2:44 am

    You have two problems here: First, your random number will always be zero. Change the line

    num = (int)Math.random()*upperBound;
    

    to

    num = (int)(Math.random()*upperBound);
    

    Your second problem is that even if you guess right on the first try, it will always ask you twice. This stems largely from the fact that you copy and pasted your guessing code. If you instead removed these lines from your code, this would not happen (NOT the ones inside the do loop):

     out.println("Enter a number between 1 and " + upperBound);
        guess = keyboard.nextInt();
        count++;
        if(guess != num)
            attempt++;
    

    Further, because of the way the loop terminates, you don’t need separate count and attempt variables. You can always predict what the attempt variable is going to be (one greater than count… well, actually, in your program, it would be two greater, but that’s not the correct guessing percentage). You could remove the attempt variable entirely, and instead do

    double countDouble = (double) count;
    pct = (int) ((countDouble/(countDouble+1))*100.0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my class: public class A{ private void doIt(int[] X, int[] Y){ //change
here is my code, SiteMember class @OneToMany(mappedBy = member,cascade=CascadeType.ALL) private List<MemberThread> memberThread = new
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my function: static Map AddFormation(Map _map, Tile tile, int x, int y, int
Here is a code snippet I was working with: int *a; int p =
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id

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.