Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am writing a code for a general guessing game in Java. The goal of the game is to guess a number from 1 to

I am writing a code for a general guessing game in Java. The goal of the game is to guess a number from 1 to 1024. The loop will continue until the user has guessed the correct number. If a number too high or too low is inputted then java will tell the user and a new prompt for a number wil be opened.

I have written out what I think will work and every thing is compiling correctly. I can enter the first number but for some reason my loop is not engaging and letting me know if the number is too high or too low. Can any one help me figure out and explain what I am doing wrong? I have included my comments on how I think the process should be running.

import java.util.Scanner; // Needed for the Scanner class import java.util.Random; // Needed for the Random class import javax.swing.JOptionPane; // Outputs shown through pop out using Panes public class DETguessingGame { public static void main(String[] args) { String inputString; // using string Random rand = new Random(); // generate random number int secret = rand.nextInt (1024) + 1; // store the random number secretly as secret int tries = 0; // number of tries int guess; // guesses Scanner input = new Scanner (System.in); boolean win = false; // begin loop statement while (win == false) // continue game till there is a winner { inputString = JOptionPane.showInputDialog( "Guess a whole number between 1 and 1024 "); guess = input.nextInt(); tries++; // display outprint in dialog box as begin guess input promting if (guess == secret) // if the guess was correct begin true statment { win = true; // game ends } else if (guess < secret) // if game has not ended output doalog box { inputString = JOptionPane.showInputDialog( "Guess was too low, try again!"); // Dialog box if number was too low } else if (guess > secret) // if the number was not too loow it is too high { inputString = JOptionPane.showInputDialog( "Guess was too High, try again!"); } } inputString = JOptionPane.showInputDialog( "Winner The secret was : " + secret + " You guessed : " + tries + "times to find the secret"); // display winning number, number of tries to get this number } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions