Java 重新启动while循环/程序

Java 重新启动while循环/程序,java,user-input,while-loop,restart,Java,User Input,While Loop,Restart,我编写了一个二叉树程序,向用户提问,以了解某些动物 我的问题是,当我的程序要求用户“继续”时,我希望循环再次开始,但我不确定如何完成。它在while循环中出现两次,在底部的学习方法中出现一次。有人对我如何做到这一点有什么想法吗 import java.util.*; public class AnimalGuessing { public static void main(String[] args) { Scanner scan = new Scanner(Sys

我编写了一个二叉树程序,向用户提问,以了解某些动物

我的问题是,当我的程序要求用户“继续”时,我希望循环再次开始,但我不确定如何完成。它在while循环中出现两次,在底部的学习方法中出现一次。有人对我如何做到这一点有什么想法吗

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
import java.util.*;
公共类动物猜测{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
TreeNode leftTree=新的TreeNode(“cat”);
TreeNode rightTree=新的TreeNode(“蛇”);
TreeNode gameTree=新的TreeNode(“它有腿吗?”,leftTree,rightTree);
TreeNodecurr=配子树;
字符串响应;
System.out.println(“”);
System.out.println(“”);
System.out.println(“想想一只动物,我会猜出来。”);
而(!(curr.getLeft()==null和&curr.getRight()==null)){
System.out.print(curr.getItem());
response=scan.nextLine();
如果(响应等于(“是”)){
curr=curr.getLeft();
System.out.print(“它是“+curr.getItem()+”?”);
response=scan.nextLine();
如果(响应等于(“是”)){
System.out.println(“我赢了!继续?”);
response=scan.nextLine();
如果(响应等于(“是”)){
//-----------------------
//我想让它重新启动
//这里是while循环!
//-----------------------
}
否则{
System.out.println(“再见”);
}
}
否则{
学习(curr);
}
}//如果结束
如果(响应等于(“否”)){
curr=curr.getRight();
System.out.println(“它是一个“+curr.getItem()+”?”);
response=scan.nextLine();
如果(响应等于(“是”)){
System.out.println(“我赢了!继续?”);
response=scan.nextLine();
如果(响应等于(“是”)){
//-----------------------
//我想让它重新启动
//这里是while循环!
//-----------------------
}
否则{
System.out.println(“再见”);
}
}
否则{
学习(curr);
}
}
}//结束时
}//端干管
//----------------------------------
//
//用于添加新动物/问题
//到树上去。
//
//----------------------------------
公共静态无效学习(TreeNode当前){
扫描仪扫描=新扫描仪(System.in);
字符串猜动物;//动物刚刚猜到
String correctAnimal;//动物用户想到的
String newQuestion;//一个用于区分这两个问题的问题
字符串响应;
//为猜出的动物、正确的动物和新问题设置字符串。
guessAnimal=current.getItem();
System.out.print(“我放弃了,这是什么?”);
correctAnimal=scan.nextLine();
System.out.println(“请键入一个问题,对于“+correctAnimal”,其答案为“是”);
System.out.print(“对于“+guessAnimal+”则为否”);
newQuestion=scan.nextLine();
//将新问题放在当前节点中,并添加两个新子项。
当前设置项(新问题);
当前.setLeft(新树节点(correctAnimal,null,null));
current.setRight(新的TreeNode(猜动物,null,null));
System.out.println(“是否继续?”);
response=scan.nextLine();
如果(响应等于(“是”)){
//--------------
//我想要它
//在这里重新开始!
//--------------
}
否则{
System.out.println(“再见”);
}
}//结束学习
}//结束动物猜测

如果是家庭作业,我不会写答案,而是解释

您想做什么来围绕代码添加一个循环

然后您需要添加一个退出循环的条件(类似于布尔循环)

这样,当您想重新开始冒险时,您可以将布尔值设置为true


如果用户已完成,则将布尔值设置为false。

如果是家庭作业,我将不写答案,而是解释

您想做什么来围绕代码添加一个循环

然后您需要添加一个退出循环的条件(类似于布尔循环)

这样,当您想重新开始冒险时,您可以将布尔值设置为true


如果用户已完成,请将布尔值设置为false。

没问题^^。我还建议你把所有东西都放在functionsHow里,你是说?比如,方法?没问题。我还建议你把所有东西都放在functionsHow里,你是说?比如,方法?