2010-12-17 19 views
1

私はバイナリツリープログラムを作成しました。このプログラムは、特定の動物について学ぶための質問をユーザーに求めています。whileループ/プログラムの再起動

私の問題は、私のプログラムがユーザーに「続行しますか?」と尋ねるときです。私はループを再開したいが、これをどうやって達成するかはわからない。私のwhileループでは2回、一番下の学習メソッドでは1回発生します。誰かが私がこれをどのように達成できるかについてのアイデアはありますか?

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 

答えて

4

その宿題は、私は答えを書きますが、あなたがそのコード

の周りにループを追加するには、それを何をしたいのか

を説明しませんならば、あなたは終了する条件を追加する必要がありますユーザーが終了した場合(ブールloopAgainのような)ループ

、あなたの冒険を再起動する場合は、この方法で、あなたはブール値をtrueに設定

にブール値を設定しますeanからfalseへ。

+0

THank you!これは大いに役立ちます。 – Bell

+1

問題ありません^^。私はまた、あなたがすべての機能を置くことをお勧めします –

+0

どういう意味ですか?メソッドのように? – Bell