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 8627957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:24:45+00:00 2026-06-12T08:24:45+00:00

for the following code, I need to stop the code by typing the word

  • 0

for the following code, I need to stop the code by typing the word “quit”, but without using “break” or “system.exit” statements. anyone can help me out? I think boolean could solve this but i have no idea how to use it.
I placed the quit statement in the first two lines of the loop,
but im not sure if it belongs there. Im in my learning phase, so dont be too strict :))

import java.util.*;

public class Game {


public static void main (String[]args){

    Game guessing = new Game();
    guessing.start();

}
public void start() {

    System.out.println("Welcome to guessing game!"); 
    System.out.println("Please enter the number between 1 and 1000");

    Scanner input = new Scanner(System.in);
    String playerName;
    String currentGuess;
    String quit = "quit";
boolean playGame = true;
int tries = 0;  //number of times player guessed

int guess = 0;  //number that player inputs

long startTime = System.currentTimeMillis();  //start timer after first guess

int randomNumber = (int) (Math.random() * 999 + 1); // generating random number
System.out.println(randomNumber);     // to be deleted after game is finished

currentGuess = input.nextLine();


do{

if (currentGuess.equalsIgnoreCase(quit)) {

        System.out.println("Thanx for playing");}


    if (currentGuess.matches("[0-9]+")) {
        int PlayerGuessInt = Integer.parseInt(currentGuess);
    }
    else {
    System.out.println("You have netered non-numeric value,please try again");
    currentGuess = input.nextLine();
    continue;
    }

    guess = Integer.parseInt(currentGuess);


    if(guess<1 || guess>1000 ){

        System.out.println("The number is out of range! Please try again");
        currentGuess = input.nextLine();
        continue;
    }

    if (guess>randomNumber){
        System.out.println("Oops,too high!");
        currentGuess = input.nextLine();
        tries++;
    }
    else if (guess<randomNumber){
        System.out.println("Sorry, to low!");
        currentGuess = input.nextLine();
        tries++;
    }



}

while (guess!=randomNumber);


    if (guess==randomNumber){
        tries++;
        long endTime = System.currentTimeMillis();
        long gameTime = endTime - startTime;
        System.out.println("Well done! You won the game in " + tries + " guesses " + "and " + gameTime/1000 +  " seconds!");
        System.out.println("Please enter your name: ");
        playerName = input.nextLine();

        }




}
}
  • 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-12T08:24:46+00:00Added an answer on June 12, 2026 at 8:24 am

    Change your while loop so that it checks for the value of current guess being “quit”. That way it will stop looping when the quit command is given e.g.

    while(guess!=randomNumber && !currentGuess.equalsIgnoreCase(quit))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code generates a MS Word document in portrait format. But I need
The following code works, but I'm curious as to why I need the Path
The following code, prints out Derived Base Base But I need every Derived object
I have following code which works for radio buttons but need to be changed
i have the following code ..i need to loop through end of the file
In the following code I need to print output from the variables $stout ,
I need syntax help with the following code logic: I have a code block
I have the following FORTRAN code which I need to convert to C or
I have the following code. I need to create list separators before A elements,
I've got the following code, and need to strip all non alpha numeric characters.

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.