2013-04-11 22 views
0

我正在製作一個簡單的基於文本的遊戲。我在我的類中有一個方法引用同一類中的另一個方法,但如果滿足某個條件,該方法會引用引用它的方法。如果該方法引用原始方法,方法可以引用同一類中的方法

所以基本上如果角色選擇打開菜單,則會調用Character.mainMenu()方法,並且該方法根據選擇的內容引用Character.invMenu()或Character.statsMenu()。

如果用戶選擇從mainMenu()中選擇Back,則調用另一個名爲backSetter()的方法。此方法採用當前的x和y座標並返回一個位置ID,以將用戶返回到它們打開菜單的任何位置。

在子菜單方法(由mainMenu()方法調用的方法)作爲invMenu(),還可以選擇返回:1.返回和2.主頁。後退選項將用戶返回到調用mainMenu()方法的主菜單。 Home選項將用戶返回到他們打開菜單的位置,調用backSetter()方法。然而問題是,如果我從invMenu()方法調用這些方法,它們似乎什麼都不做,我的程序就凍結了。

這裏是我的Character類的基本模板(這裏引用的其他類是MainClass,它包含setLocation()方法和引用gameText()方法的GameText類)。

class Character{ 

    static Scanner userInput = new Scanner(); 
    static String choice; 
    static int backChoice = 0; 

    public static void backSetter(){ 
     if (xAxis == 0 && yAxis == 0){ 
      backChoice = MainClass.setLocation(0,0); 
      GameText.gameText(backChoice); 
     }else if (xAxis == 0 && yAxis == 1){ 
      ..... 
     } 
    } 

    public static void mainMenu(){ 
     //My menu options: 1. Inventory, 2. Stats, etc... 5. BACK <- important 
     choice = userInput.next(); 

     if (choice.equals("1")){ 
      invMenu(); 
     }else if (choice.equals("5")){ 
      backSetter(); 
    } 

    public static void invMenu(){ 
     // Determines current items in inventory and prints them out 
     //My menu options: 1. Back, 2. Home <- where the problem happens 
     choice = userInput.next(); 

     if (choice.equals("1")){ 
      mainMenu(); 
     }else if (choice.equals("2")){ 
      backSetter(); 
     } 
    } 

} 
+0

是的,這是完全合法的,將編譯。它被稱爲「間接遞歸」。儘管如此,你必須非常小心,有一些方法可以退出遞歸。換句話說,你很容易陷入無限循環。 – 2013-04-11 02:10:05

回答

1

是的,這是合法的 - 這是什麼意思的方法是recursive。確保你沒有無限遞歸的情況,即確保你的方法最終不會互相調用 - 如果遞歸最終終止,那麼你很好。

0

是的,這幾乎是一個遞歸的例子。它不會產生任何錯誤。 但是,因爲它是遞歸的,所以如果它們沒有達到基本情況狀態,它可能會導致無限循環。 在這種情況下,方法互相調用,乍一看遞歸不明顯,無限循環更有可能。

以某種方式隔離該遞歸可能不是一個壞主意,因此它更明顯,使代碼更清潔,更容易維護。

0

是的,這是合法的,但它是非常糟糕的設計。遞歸菜單調用可能會導致一些麻煩。我覺得回報會更好,但是我不知道這段代碼的上下文。

public static void invMenu(){ 
    // Determines current items in inventory and prints them out 
    //My menu options: 1. Back, 2. Home <- where the problem happens 
    choice = userInput.next(); 

    if (choice.equals("1")){ 
     return; 
    }else if (choice.equals("2")){ 
     backSetter(); 
    } 
} 

如果你是去這樣做,你就需要調整主菜單來處理回報(即一個某種循環)的代碼。

0

感謝您的幫助,我認爲這可能會導致無限循環。是的,如果沒有完整的代碼,很難發現我要出錯的地方。我想添加這個作爲評論,但它是太多的字符,所以我已經添加它作爲一個答案。

我一直在看我的方法,我看不到它是如何陷入無限循環。調用方法2的方法1必須在確定要執行的操作之前採取用戶輸入。因此,如果Methpd1再次調用方法2調用方法2,爲什麼它會卡住,因爲用戶可以選擇以任一方法返回到正常遊戲。這是我的方法,也許看到它完全可以揭示我要出錯的地方。

The FantasyGameText。gameText方法只需要locID並輸出該位置的對話選項。 FantasyGameText.setLocation方法是設置位置ID的實際方法,backSetter方法簡單地將它與gameText方法結合起來(基本上和我開始我的遊戲一樣,xAxis = 0,yAxis = 0,所以locId = 0,這就是我的比賽的開始部分)。

public static void backSetter(){ 
    if (xAxis == 2 && yAxis == 1){ 
     backChoice = setLocation(2, 1); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 4 && yAxis == 1){ 
     backChoice = setLocation(4, 1); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 2 && yAxis == 2){ 
     backChoice = setLocation(2, 2); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 3 && yAxis == 2){ 
     backChoice = setLocation(3, 3); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 4 && yAxis == 2){ 
     backChoice = setLocation(4, 2); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 5 && yAxis == 2){ 
     backChoice = setLocation(5, 2); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 1 && yAxis == 3){ 
     backChoice = setLocation(1, 3); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 2 && yAxis == 3){ 
     backChoice = setLocation(2, 3); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 3 && yAxis == 3){ 
     backChoice = setLocation(3, 3); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 4 && yAxis == 3){ 
     backChoice = setLocation(4, 3); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 2 && yAxis == 4){ 
     backChoice = setLocation(2, 4); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 3 && yAxis == 4){ 
     backChoice = setLocation(3, 4); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 4 && yAxis == 4){ 
     backChoice = setLocation(4, 4); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 5 && yAxis == 4){ 
     backChoice = setLocation(5, 4); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 3 && yAxis == 5){ 
     backChoice = setLocation(3, 5); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 4 && yAxis == 5){ 
     backChoice = setLocation (4, 5); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 30 && yAxis == 30){ 
     backChoice = setLocation(30, 30); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 31 && yAxis == 31){ 
     backChoice = setLocation(31, 31); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 50 && yAxis == 50){ 
     backChoice = setLocation(50, 50); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 51 && yAxis == 51){ 
     backChoice = setLocation(51, 51); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 52 && yAxis == 52){ 
     backChoice = setLocation(52, 52); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 53 && yAxis == 53){ 
     backChoice = setLocation(53, 53); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 54 && yAxis == 54){ 
     backChoice = setLocation(54, 54); 
     FantasyGameText.gameText(backChoice); 
    }else if (xAxis == 55 & yAxis == 55){ 
     backChoice = setLocation(55, 55); 
     FantasyGameText.gameText(backChoice); 
    }else{ 
     FantasyGameText.gameText(100); 
    } 
} 

public static void menuHome(){ 
    boolean correctAnswer = false; 
    System.out.println(""); 
    System.out.println("-------- CHARACTER --------"); 
    System.out.println("1. Inventory"); 
    System.out.println("2. Equip"); 
    System.out.println("3. Stats"); 
    System.out.println("4. Quests"); 
    System.out.println("5. Back"); 
    while (correctAnswer == false){ 
     menuChoice = userInput.next(); 
     System.out.println(""); 
     if (menuChoice.equals("1") || menuChoice.equalsIgnoreCase("inventory") || menuChoice.equalsIgnoreCase("inv")){ 
      correctAnswer = true; 
      invMenu(); 
     }else if (menuChoice.equals("2") || menuChoice.equalsIgnoreCase("equip")){ 
      correctAnswer = true; 
      equipMenu(); 
     }else if (menuChoice.equals("3") || menuChoice.equalsIgnoreCase("stats") || menuChoice.equalsIgnoreCase("statistics")){ 
      correctAnswer = true; 
      statsMenu(); 
     }else if (menuChoice.equals("4") || menuChoice.equalsIgnoreCase("quests") || menuChoice.equalsIgnoreCase("quest")){ 
      correctAnswer = true; 
      questMenu(); 
     }else if (menuChoice.equals("5") || menuChoice.equalsIgnoreCase("back") || menuChoice.equalsIgnoreCase("home")){ 
      correctAnswer = true; 
      backSetter(); 
     }else{ 
      System.out.println("That is not a valid option, choose again,"); 
      menuChoice = userInput.next(); 
      System.out.println(""); 
     } 
    } 
return; 
} 

public static void invMenu(){ 
    boolean correctAnswer = false; 
    int invList[] = new int[50]; 
    int itemAmount[] = new int[50]; 
    for (int i = 0; i <= 3; i++){ 
     if (healthPotionAmount > 0){ 
      invList[0] = 1; 
      itemTempName[0] = "Health Potion x "; 
      itemAmount[0] = healthPotionAmount; 
     }else{ 
      invList[0] = 0; 
     } 
     if (rustySwordAmount > 0){ 
      invList[1] = 1; 
      itemTempName[1] = "Rusty Sword x "; 
      itemAmount[1] = rustySwordAmount; 
     }else{ 
      invList[1] = 0; 
     } 
     if (ragsBodyAmount > 0){ 
      invList[2] = 1; 
      itemTempName[2] = "Rags x "; 
      itemAmount[2] = ragsBodyAmount; 
     }else{ 
      invList[2] = 0; 
     } 
     if (chainBodyAmount > 0){ 
      invList[3] = 1; 
      itemTempName[3] = "Chainmail Armor x "; 
      itemAmount[3] = chainBodyAmount; 
     }else{ 
      invList[3] = 0; 
     } 
    } 
    for (int i = 0; i <= 3; i++){ 
     if (invList[i] > 0){ 
      System.out.println(itemTempName[i] + itemAmount[i]); 
     }else if (invList[0] == 0 && invList[1] == 0 && invList[2] == 0 && invList[3] == 0){ 
      System.out.println("You have no items."); 
     } 
    } 
    System.out.println(""); 
    System.out.println("1. Back"); 
    System.out.println("2. Home"); 
    menuChoice = userInput.next(); 
    System.out.println(""); 
    while (correctAnswer == false); 
    if (menuChoice.equals("1") || menuChoice.equalsIgnoreCase("back") || menuChoice.equalsIgnoreCase("b")){ 
     correctAnswer = true; 
     menuHome(); 
    }else if (menuChoice.equals("2") || menuChoice.equalsIgnoreCase("home") || menuChoice.equalsIgnoreCase("h")){ 
     correctAnswer = true; 
     backSetter(); 
    }else{ 
     System.out.println("That is not a valid option, choose again."); 
     menuChoice = userInput.next(); 
     System.out.println(""); 
    } 
} 
相關問題