1

我正在用 java 编写一个程序,它基本上知道各种海洋动物,让用户想一种动物,然后问用户问题,直到它准备好进行猜测。我使用二叉树来做到这一点。这是我现在的代码:

import java.util.Scanner;
public class TwentyQuestions
{
   private static Scanner stdin = new Scanner(System.in);
   public static void main(String[ ] args)
   {
      BTNode<String> root;

      instruct( );
      root = beginningTree( );
      do
         play(root);
      while (query("Shall we play again?"));

      System.out.println("Thanks for teaching me a thing or two.");
      System.out.println ("Here is the tree:");
      root.print(1);
   }

   public static void instruct( )
   {
      System.out.println("Please think of an ocean animal.");
      System.out.println("I will ask some yes/no questions to try to figure out which animal you're thinking of.");
   }


   public static void play(BTNode<String> current)
   {
      while (!current.isLeaf( ))
      {
         if (query(current.getData( )))
            current = current.getLeft( );
         else
            current = current.getRight( );
      }

      System.out.print("My guess is " + current.getData( ) + ". ");
      if (!query("Am I right?"))
         learn(current);
      else
         System.out.println("I knew it all along!");
   }

   public static BTNode<String> beginningTree( )   
   {
      BTNode<String> root;
      BTNode<String> child;
      BTNode<String> child1;
      BTNode<String> child2;
      BTNode<String> child3;
      BTNode<String> child4;
      BTNode<String> child5;
      BTNode<String> child6;
      BTNode<String> child7;
      BTNode<String> child8;
      BTNode<String> child9;
      BTNode<String> child10;
      BTNode<String> child11;
      BTNode<String> child12;
      BTNode<String> child13;
      BTNode<String> child14;

      final String ROOT_QUESTION = "Is it a mammal?";
      final String LEFT_QUESTION = "Is it able to move on land?";
      final String LEFT_QUESTION2 = "Is it a solitary animal?";
      final String RIGHT_QUESTION2 = "Is it larger than a truck?";
      final String RIGHT_QUESTION3 = "Does it have tusks?";
      final String RIGHT_QUESTION = "Does it have any limbs/tentacles?";
      final String LEFT_QUESTION4 = "Does it have more than four limbs/tentacles?";
      final String LEFT_QUESTION5 = "Does it have an exoskeleton?";
      final String LEFT_QUESTION6 = "Does it have claws?";
      final String LEFT_QUESTION7 = "Does it have a long tail?";
      final String RIGHT_QUESTION7 = "Does it have 8 arms?";
      final String RIGHT_QUESTION5 = "Does it have a shell?";
      final String RIGHT_QUESTION4 = "Can it sting?";
      final String LEFT_QUESTION8 = "Is it long and snakelike?";
      final String RIGHT_QUESTION8 = "Is it generally smaller than a car?";
      final String ANIMAL1 = "Seal";
      final String ANIMAL2 = "Sea Lion";
      final String ANIMAL3 = "Walrus";
      final String ANIMAL4 = "Whale";
      final String ANIMAL5 = "Dolphin";
      final String ANIMAL6 = "Shrimp";
      final String ANIMAL7 = "Lobster";
      final String ANIMAL8 = "Crab";
      final String ANIMAL9 = "Jellyfish";
      final String ANIMAL10 = "Octopus";
      final String ANIMAL11 = "Squid";
      final String ANIMAL12 = "Turtle";
      final String ANIMAL13 = "Alligator";
      final String ANIMAL14 = "Eel";
      final String ANIMAL15 = "Stingray";
      final String ANIMAL16 = "Shark";
      final String ANIMAL17 = "Fish";

      // Create the root node with the question “Are you a mammal?”
      root = new BTNode<String>(ROOT_QUESTION, null, null);

      child = new BTNode<String>(LEFT_QUESTION, child2, child14);
      root.setLeft(child);

      child2 = new BTNode<String>(LEFT_QUESTION2,null,child3);
      child2.setLeft(new BTNode<String>(ANIMAL1, null, null));
      child.setLeft(child2);

      child14 = new BTNode<String>(RIGHT_QUESTION2,null,null);
      child14.setLeft(new BTNode<String>(ANIMAL4,null,null));
      child14.setRight(new BTNode<String>(ANIMAL5,null,null));
      child.setRight(child14);

      child3 = new BTNode<String>(RIGHT_QUESTION3, null, null);
      child3.setLeft(new BTNode<String>(ANIMAL3, null, null));
      child3.setRight(new BTNode<String>(ANIMAL2, null, null));
      child.setRight(child3);

      child1 = new BTNode<String>(RIGHT_QUESTION, child4, child8);
      root.setRight(child1);

      child4 = new BTNode<String>(LEFT_QUESTION4,child5,child10);
      child1.setLeft(child4);

      child5 = new BTNode<String>(LEFT_QUESTION5,child6,child8);
      child4.setLeft(child5);

      child6 = new BTNode<String>(LEFT_QUESTION6,child7, null);
      child6.setRight(new BTNode<String>(ANIMAL6,null,null));
      child5.setLeft(child6);

      child7 = new BTNode<String>(LEFT_QUESTION7, null, null);
      child7.setLeft(new BTNode<String>(ANIMAL7,null,null));
      child7.setRight(new BTNode<String>(ANIMAL8,null,null));
      child6.setLeft(child7);

      child8 = new BTNode<String>(RIGHT_QUESTION4,null,child9);
      child8.setLeft(new BTNode<String>(ANIMAL9,null,null));
      child5.setRight(child8);

      child9 = new BTNode<String>(RIGHT_QUESTION7,null,null);
      child9.setLeft(new BTNode<String>(ANIMAL10,null,null));
      child9.setRight(new BTNode<String>(ANIMAL11,null,null));
     child8.setRight(child9);

      child10 = new BTNode<String>(RIGHT_QUESTION5,null,null);
      child10.setLeft(new BTNode<String>(ANIMAL12,null,null));
      child10.setRight(new BTNode<String>(ANIMAL13,null,null));
      child4.setRight(child10);

      child11 = new BTNode<String>(RIGHT_QUESTION4,child12,child13);
      child1.setRight(child11);

      child12 = new BTNode<String>(LEFT_QUESTION8,null,null);
      child12.setLeft(new BTNode<String>(ANIMAL14,null,null));
      child12.setRight(new BTNode<String>(ANIMAL15,null,null));
     child11.setLeft(child12);

      child13 = new BTNode<String>(RIGHT_QUESTION8,null,null);
      child13.setLeft(new BTNode<String>(ANIMAL17,null,null));
      child13.setRight(new BTNode<String>(ANIMAL16,null,null));
      child11.setRight(child13);

      return root;
   }


   public static void learn(BTNode<String> current)

   {
      String guessAnimal;   // The animal that was just guessed
      String correctAnimal; // The animal that the user was thinking of
      String newQuestion;   // A question to distinguish the two animals

      // Set Strings for the guessed animal, correct animal and a new question.
      guessAnimal = current.getData( );
      System.out.println("I give up. What are you? ");
      correctAnimal = stdin.nextLine( );
      System.out.println("Please type a yes/no question that will distinguish a");
      System.out.println(correctAnimal + " from a " + guessAnimal + ".");
      newQuestion = stdin.nextLine( );

      // Put the new question in the current node, and add two new children.
      current.setData(newQuestion);
      System.out.println("As a " + correctAnimal + ", " + newQuestion);
      if (query("Please answer"))
      {
         current.setLeft(new BTNode<String>(correctAnimal, null, null));
         current.setRight(new BTNode<String>(guessAnimal, null, null));
      }
      else
      {
         current.setLeft(new BTNode<String>(guessAnimal, null, null));
         current.setRight(new BTNode<String>(correctAnimal, null, null));
      }         
   }

   public static boolean query(String prompt)
   {
      String answer;

      System.out.print(prompt + " [Y or N]: ");
      answer = stdin.nextLine( ).toUpperCase( );
      while (!answer.startsWith("Y") && !answer.startsWith("N"))
      {
    System.out.print("Invalid response. Please type Y or N: ");
    answer = stdin.nextLine( ).toUpperCase( );
      }

      return answer.startsWith("Y");
   }

}

不断出现的错误是“变量子(+无论数字)可能尚未初始化。” 我该如何解决?

哦,这是 BTNode 的代码:

 public class BTNode<E>
    {
       private E data;
       private BTNode<E> left, right;   
       public BTNode(E initialData, BTNode<E> initialLeft, BTNode<E> initialRight)
       {
          data = initialData;
          left = initialLeft;
          right = initialRight;
       }       

       public E getData( )   
       {
          return data;
       }



   public BTNode<E> getLeft( )
   {
      return left;                                               
   } 



   public E getLeftmostData( )
   {
      if (left == null)
         return data;
      else
         return left.getLeftmostData( );
   }


   public BTNode<E> getRight( )
   {
      return right;                                               
   } 


   public E getRightmostData( )
   {
      if (left == null)
         return data;
      else
         return left.getRightmostData( );
   }

   public void inorderPrint( )
   {
      if (left != null)
         left.inorderPrint( );
      System.out.println(data);
      if (right != null)
         right.inorderPrint( );
   }  


   public boolean isLeaf( )
   {
      return (left == null) && (right == null);                                               
   } 


   public void preorderPrint( )
   {
      System.out.println(data);
      if (left != null)
         left.preorderPrint( );
      if (right != null)
         right.preorderPrint( );
   } 

   public void postorderPrint( )
   {
      if (left != null)
         left.postorderPrint( );
      if (right != null)
         right.postorderPrint( );
      System.out.println(data);
   }   

   public void print(int depth)
   {
      int i;

      // Print the indentation and the data from the current node:
      for (i = 1; i <= depth; i++)
         System.out.print("    ");
      System.out.println(data);

      if (left != null)
         left.print(depth+1);
      else if (right != null)
      {
         for (i = 1; i <= depth+1; i++)
            System.out.print("    ");
         System.out.println("--");
      }

      if (right != null)
         right.print(depth+1);
      else if (left != null)
      {
         for (i = 1; i <= depth+1; i++)
            System.out.print("    ");
         System.out.println("--");
      }
   }

   public BTNode<E> removeLeftmost( )
   {
      if (left == null)
         return right;
      else
      {
         left = left.removeLeftmost( );
         return this;
      }
   }

   public BTNode<E> removeRightmost( )
   {
      if (right == null)
         return left;
      else
      {
         right = right.removeRightmost( );
         return this;
      }
   }

   public void setData(E newData)   
   {
      data = newData;
   }                                                               


   public void setLeft(BTNode<E> newLeft)
   {                    
      left = newLeft;
   }



   public void setRight(BTNode<E> newRight)
   {                    
      right = newRight;
   }  

   public static <E> BTNode<E> treeCopy(BTNode<E> source)
   {
      BTNode<E> leftCopy, rightCopy;

      if (source == null)
         return null;
      else
      {
         leftCopy = treeCopy(source.left);
         rightCopy = treeCopy(source.right);
         return new BTNode<E>(source.data, leftCopy, rightCopy);
      }
   }

   public static <E> long treeSize(BTNode<E> root)
   {
      if (root == null)
         return 0;
      else
         return 1 + treeSize(root.left) + treeSize(root.right);
   }   

}
4

1 回答 1

1

声明变量时,请确保在使用它们之前也对其进行初始化。也就是说,而不是写

BTNode<String> root;
BTNode<String> child;
BTNode<String> child1;
...

BTNode<String> root = null;
BTNode<String> child = null;
...

这是因为当您稍后继续编写语句时

child = new BTNode<String>(LEFT_QUESTION, child2, child14);

child2 和 child14 将被初始化(而在您的情况下,它们仅被声明,未初始化)

于 2013-04-23T01:06:30.333 回答