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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:47:56+00:00 2026-05-31T04:47:56+00:00

1) The first loop I need to use is a do/while. In this loop

  • 0

1) The first loop I need to use is a do/while. In this loop the user is prompted to type in a word. If the user types in the wrong word they get an error message:

Invalid! Try Again! 2 attempt(s) left!

Invalid! Try Again! 1 attempt(s) left!

Sorry! You have no more attempts left!

This output works as long as the wrong word is entered each time, but if the correct word is entered after 1 failed attempt it still applies the error message

2) Within my second loop (for loop) the user is asked to enter “3 * 8 = ” This portion of the loop works fine if a wrong number is entered all 3 times or if 24 is entered on any attempt.

The problem lies in the loop after 24 is entered. The output is as follows:

Thank you blah blah. We’ll call you at 5555555555 if you’re a winner. 3 * 8 = Where the 3 * 8 should not be showing. I realize I could enter a break; after this statement, but the instructions specifically say that I cannot use the break command.

The correct output should read: Thank you blah blah. We’ll call you at 5555555555 if you’re a winner.

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);

    int attempt = 2;
    int answer = 24;
    long phoneNumber = 0;
    String firstName = "";
    String lastName = "";
    String wordOfTheDay = "";

    System.out.printf("Enter the word of the day:  ");
    wordOfTheDay = input.nextLine();

    if(wordOfTheDay.equals("tired"))
    {
        for( attempt = 2; attempt >= 0; --attempt)
        {
            System.out.print(" 3 * 8 = ");
            answer = input.nextInt();
            input.nextLine();

            if( answer == 24)
            {
                System.out.printf( "Please enter your first name, last name, and phone number (no dashes or spaces)\n"                              +"in a drawing for an all-expenses-paid vacation in the Bahamas: " );

                firstName = input.next();
                lastName = input.next();
                phoneNumber = input.nextLong();

                System.out.printf(
                    "Thank you %s %s. We'll call you at %d if you're a winner.",
                    firstName,
                    lastName,
                    + phoneNumber);
            }

            else if( answer != 24)
            {
                if(attempt!=0)
                {
                    System.out.printf( "Invalid! Try Again! %d attempt(s) left!\n ", attempt);
                    continue;
                }
                else
                {
                    System.out.print( "Sorry!  You have no more attempts left!" );
                }
            }
        }
    }
    else
    {
        do
        {
            System.out.printf( "Invalid! Try Again! %d attempt(s) left!\n ", attempt);
            --attempt;
            System.out.printf("Enter the word of the day:  ");
            wordOfTheDay = input.nextLine();
        } while (attempt >= 1);

        if( attempt == 0)
        {
            System.out.print( "Sorry!  You have no more attempts left!" );
        }
    }
}

I hope I made this clear enough.

To recap, I need to fix the problem with my do/while not letting me enter the correct word after a failed attempt.

Also, I need to get rid of the 3 * 8 = showing up after the users enters the correct input.

  • 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-05-31T04:47:58+00:00Added an answer on May 31, 2026 at 4:47 am

    Normally, you would use the break statement, but as you are not allowed setting attempt = -1 will have the same effect:

    if( answer == 24)
    {
        ...
        attempt = -1; // An ugly 'break'
    }
    

    EDIT:

    Move the do { } while(); to before if check:

        // These two lines of code are no longer required.
        //System.out.printf("Enter the word of the day:  ");
        //wordOfTheDay = input.nextLine();
    
        do
        {
            System.out.printf("Enter the word of the day:  ");
            wordOfTheDay = input.nextLine();
    
            if(!wordOfTheDay.equals("tired"))
            {
                System.out.printf(
                    "Invalid! Try Again! %d attempt(s) left!\n ", --attempt);
            }
            else
            {
                attempt = 0; // Another ugly 'break'
            }
        } while (attempt >= 1);
    
    
        if(wordOfTheDay.equals("tired"))
        {
        }
        // Remove else branch as not required.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've been stuck in this loop for a while. Basically i have a slider
This is my situation: i need the code to run in a loop, over
first time posting here. I'm using a loop to create 3 buttons, but my
First time poster here. A quick question about setting up a loop here. I
I'm confused. I'm trying to loop though 2 files looking at the first token
First, let's get the security considerations out of the way. I'm using simple authentication
First off, there's a bit of background to this issue available on my blog:
I'm using coldfusion and jquery. This is my first real go at jquery and
I use a scheduler to ensure that my loop does not take too long,
I haven't use PHP for some time, and I need some help to create

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.