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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:00:15+00:00 2026-05-18T20:00:15+00:00

I’ve written a Binary Tree program that asks the user questions to learn about

  • 0

I’ve written a Binary Tree program that asks the user questions to learn about certain animals.

My problem is that when my program asks the user “Continue?” I want the loop to begin again but I am not sure how to accomplish this. It occurs twice in my while loop and once in my learn method at the bottom. Does anybody have any ideas about how I can accomplish this?

import java.util.*;

public class AnimalGuessing  {

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


    TreeNode<String> leftTree = new TreeNode<String> ("cat");
    TreeNode<String> rightTree = new TreeNode<String> ("snake");
    TreeNode<String> gameTree = new TreeNode<String>( "Does it have legs?",leftTree,rightTree); 

    TreeNode<String>curr = gameTree;
    String response;
    System.out.println("");
    System.out.println("");
    System.out.println("Think of an animal and I will guess it.");

    while(!(curr.getLeft() == null && curr.getRight()==null)){
        System.out.print(curr.getItem());
        response = scan.nextLine();
        if (response.equals("YES")){
            curr = curr.getLeft();

            System.out.print("Is it a " + curr.getItem() + "?");
            response = scan.nextLine();
            if (response.equals("YES")){
                System.out.println("I win! Continue?");
                response = scan.nextLine();
                if (response.equals("YES")){
                    //-----------------------
                    // I want it to restart
                    // the while loop here!
                    //-----------------------
                }
                else{
                    System.out.println("Good-bye.");
                }
            }
            else{
                learn(curr);
            }

        }//end if
        if (response.equals("NO")){
            curr = curr.getRight();
            System.out.println("is it a " + curr.getItem() + "?");
            response = scan.nextLine();
            if (response.equals("YES")){
                System.out.println("I win! Continue?");
                response = scan.nextLine();
                if (response.equals("YES")){
                    //-----------------------
                    // I want it to restart
                    // the while loop here!
                    //-----------------------
                }
                else{
                    System.out.println("Good-bye.");
                }
            }
            else{
                learn(curr);
            }
        }
    }//end while

}//end main

    //----------------------------------
    //
    // Use to add new animals/questions
    // to the tree.
    //
    //----------------------------------
    public static void learn(TreeNode<String> current){
        Scanner scan = new Scanner(System.in);
          String guessAnimal;   // animal just guessed guessed
          String correctAnimal; // animal user was thinking of
          String newQuestion;   // A question to distinguish the two
          String response;

          // Set Strings for the guessed animal and correct animal and a new question.
          guessAnimal = current.getItem();
          System.out.print("I give up. What is it? ");
          correctAnimal = scan.nextLine( );
          System.out.println("Please type a question whose answer is yes for a " +correctAnimal);
          System.out.print("and no for a " + guessAnimal + ".");
          newQuestion = scan.nextLine( );


          // Put the new question in the current node, and add two new children.
          current.setItem(newQuestion);
                current.setLeft(new TreeNode<String>(correctAnimal, null, null));
                current.setRight(new TreeNode<String>(guessAnimal, null, null));

          System.out.println("Continue?");
          response = scan.nextLine();
              if (response.equals("YES")){
                //--------------
                //I want it to
                //restart here!
                //--------------

              }
              else{
                  System.out.println("Good-bye.");
              }
       }//end learn 







}//end AnimalGuessing
  • 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-18T20:00:15+00:00Added an answer on May 18, 2026 at 8:00 pm

    if its homework I won’t write the answer but explain

    What you want to do it to add a loop around that code

    then you need to add a condition to exit the loop (like a boolean loopAgain)

    this way when you want to restart your adventure, you set the boolean to true

    if the user has finished you set the boolean to false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.