Java Guessing Game, computer science homework help

I’m having a few problems with a java guessing game. I’m supposed to have my code think up a random number and then prompt the user to input what I’m thinking until they get it right. I have a bug in my program because it always says the number is lower whether it is higher or lower but when I input the max number, it says I got it right. I don’t know where I went wrong. Any help is much appreciated! I’m going to link my code and the assignment specifications: http://courses.cs.washington.edu/courses/cse142/16wi/homework/5/spec.pdf 

import java.util.*;

public class GuessingGame {

   public static void main(String[] args) {

      game();

      statistics();

   }

   public static void game() {

      Random random = new Random();

      Scanner console = new Scanner(System.in);

      int num = random.nextInt(50);

      int guess = 0;

      int guess_count = 0;

      System.out.println(“I’m thinking of a number between 1 and ” + num);

      while (guess != num) {

         System.out.println(“Your guess? “);

         guess = console.nextInt();

         guess_count++;

         if (guess > num) {

            System.out.println(“It’s higher.”);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else if (guess < num) {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(“It’s lower.”);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(“You got it right in ” + guess_count + ” guesses”);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

&nbsp; &nbsp; &nbsp; } &nbsp;

&nbsp; &nbsp; &nbsp; System.out.println(“Do you want to play again? “);

&nbsp; &nbsp; &nbsp; System.out.println();

&nbsp; &nbsp;}

&nbsp; &nbsp;public static void statistics() {

&nbsp; &nbsp; &nbsp; System.out.println(“Overall results:”);

&nbsp; &nbsp; &nbsp; System.out.println(“Total games = “);

&nbsp; &nbsp; &nbsp; System.out.println(“Total guesses = “);

&nbsp; &nbsp; &nbsp; System.out.println(“Guesses/game = “);

&nbsp; &nbsp; &nbsp; System.out.println(“Best game = “);

&nbsp; &nbsp;}

}


Leave a Comment

Your email address will not be published. Required fields are marked *

Is this question part of your Assignment?

Get expert help

Girl in a jacket


We are a team of academic consultants with extensive experience in writing academic papers for college students in the US, Canada, UK, AU, and other parts of the world.

We help students with both technical and non-technical assignments across all majors & academic disciplines.

Unlike what our name suggests, we research and draft everything word for word. We do not use AI or any rewriting tool! We provide Turnitin reports for AI & Turnitin alongside every paper.

Need help? Send us your assignment now!

description here description here description here