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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:38:01+00:00 2026-06-12T20:38:01+00:00

I was having trouble getting the do-while loop to stop after the user has

  • 0

I was having trouble getting the do-while loop to stop after the user has attempted to guess the number 7 times. I’m not sure exactly how to tell the loop to stop and display the timeout message.

Thank you, I appreciate any help!

Code:

import java.util.Random;
import java.util.Scanner;
public class numberGuesses {
     public static void main (String [] args){
            Random rand = new Random();
            int inputNumber;
            int randomNumber = rand.nextInt(100) + 1;
            int guessingLimit = 7;

            System.out.println("Rand Number = " +randomNumber); //Temp to test program with correct number 


            Scanner scan = new Scanner (System.in);

            do {
                System.out.print("Please enter a number: ");
                inputNumber = scan.nextInt();

                if (inputNumber > randomNumber)
                            System.out.println("That's too high!");
                if (inputNumber < randomNumber)
                            System.out.println("That's too low!");
       //       else { 
       //       guessingLimit--;
       //       System.out.println("Too bad! The number was " +randomNumber+ "!"); //Trying to have it print this timeout after 7 incorrect attempts
       //       break;
       //       }
            } while (inputNumber != randomNumber);
                System.out.println("That's right!"); //This line when they guess correctly  
    }

}

Correct Output 1:

----exec: java numberGuesses
Rand Number = 67
Please enter a number: 89
That's too high!
Please enter a number: 1
That's too low!
Please enter a number: 67
That's right!

Correct Output 2:

----exec: java numberGuesses
Rand Number = 38
Please enter a number: 55
That's too high!
Please enter a number: 66
That's too high!
Please enter a number: 1
That's too low!
Please enter a number: 22
That's too low!
Please enter a number: 44
That's too high!
Please enter a number: 8
That's too low!
Please enter a number: 99
That's too high!
Too bad! The number was 38!
  • 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-12T20:38:03+00:00Added an answer on June 12, 2026 at 8:38 pm

    Before do, write,

      int numAttempts = 0;
    

    within do

      numAttempts++;
    

    while condition:

      while (inputNumber != randomNumber && numAttempts < 7);
    

    Complete:

           int numAttempts = 0;
           do {
                System.out.print("Please enter a number: ");
                inputNumber = scan.nextInt();
    
                if (inputNumber > randomNumber)
                            System.out.println("That's too high!");
                if (inputNumber < randomNumber)
                            System.out.println("That's too low!");
       //       else { 
       //       guessingLimit--;
       //       System.out.println("Too bad! The number was " +randomNumber+ "!"); //Trying to have it print this timeout after 7 incorrect attempts
       //       break;
       //       }
              numAttempts++;
            } while (inputNumber != randomNumber && numAttempts < 7);
    
            if(inputNumber == randomNumber){
                System.out.println("That's right!"); 
            }else{
                System.out.println("Incorrect 7 attempts"); 
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble getting the correct sorted list. I am not sure what
For some reason I'm having trouble getting my program to run a while loop.
I am having trouble getting a method working and I am not sure I
I'm having trouble getting my cin loop to terminate in my program. My program
I'm having trouble getting the hang of the character encoding while dealing with QWebKit's
I am having some trouble with my jquery. I am not sure if it
Having trouble getting scheduled tasks to run at a specified future time while using
I am having trouble getting a custom message while still getting the error classes
I am having trouble getting Accelerators to work. I am using C++. After my
I am not too experienced with Java and am having trouble getting a progress

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.