重新启动while循环/程序

问题描述:

我写了一个二叉树程序,要求用户提问以了解某些动物。重新启动while循环/程序

我的问题是,当我的程序询问用户“继续?”我想循环再次开始,但我不知道如何完成此操作。它在我的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 

如果功课,我不会写答案,而是要解释

你想要做它周围添加该代码回路什么

那么你需要添加一个条件退出环(像一个布尔loopAgain)

当你要重新启动你的冒险这种方式,您设置的布尔为true

如果用户已经完成了你设定的布尔很虚伪。

+0

谢谢你!这有很大帮助。 – Bell 2010-12-17 03:15:15

+1

没问题^^。我也建议你把所有的东西都放在功能上 – 2010-12-17 03:20:57

+0

你是什么意思?像,方法? – Bell 2010-12-17 03:26:48