2010-12-17 3 views
1

나는 특정 동물에 대해 배우기 위해 사용자에게 질문하는 바이너리 트리 프로그램을 작성했습니다.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 

답변

4

의 숙제는 내가 답을 쓰기 만

는 당신이를 종료 조건을 추가 할 필요가 당신이 그 코드 주위에 루프를 추가하여 수행 할 작업을

을 설명하지 않을 경우 사용자가 완료된 경우 (부울 loopAgain 같은) 루프

당신이 당신의 모험을 다시 시작하려면이 방법, 당신은 부울을 true로 설정

에 부울을 설정 거짓으로 거짓말.

+0

너! 이것은 많은 도움이됩니다. – Bell

+1

문제 없어요 ^^. 나는 또한 모든 것을 기능에 넣으라고 제안합니다. –

+0

당신은 어떻게 의미합니까? 방법? – Bell