首页 > 解决方案 > 让一个方法在我的游戏的方法 java 中运行?

问题描述

我正在为我的游戏处理库存扫描而苦苦挣扎,如果存在“飞天扫帚”,它基本上会搜索用户库存(它是用另一种方法收集的,上传代码太长),如果没有,它将运行该方法再次挑战dragon();否则,如果该项目存在,它将继续进行下一个挑战。我认为插入方法作为参数,但这是不可能的。这就是我现在所拥有的。:

public class Main {

    String Flyingbroom = "Flying broom";

    public static void main(String[] args) {
        Player_inventory p = new Player_inventory();
        challengedragon();
    }

public void challengedragon() {


    System.out.println("a Hungarian Horntail dragon! Let's start the battle! You have four options to beat the dragon: ");
    System.out.println("1: Fly away with your broom");
    System.out.println("2: Fight the dragon");
    System.out.println("3: Just run to the egg and get it");
    System.out.println("4: Hide behind a rock");
    System.out.println("5: Go back to Hogwart");



    System.out.println("Your choice is: ");

    Scanner in = new Scanner(System.in);
    int dragonfightchoice = in .nextInt();

    if (dragonfightchoice == 1) {
      {
        p.Scanitem(Flyingbroom,
          "Good choice! You managed to kill the Hungarian Horntail dragon and to get the golden egg",
          "You dont have the broom. Try to search for the broom",
          playerHP);
        proceedtonextchallengelake();

      } else if (dragonfightchoice == 2) {
        System.out.println("The Hungarian Horntail dragon fired you. - 70HP. ");
        playerHP -= 70;
        challengedragon();
      } else if (dragonfightchoice == 3) {
        System.out.println("Bad idea... You lose 100 HP");
        playerHP -= 100;
        challengedragon();
      } else if (dragonfightchoice == 4) {
        System.out.println("The dragon found you. You lose 30 HP");
        playerHP -= 30;
        challengedragon();
      } else if (dragonfightchoice == 5) {
        Hogwart();
      } else {
        invalid();
        challengedragon();
      }
    }

对于我的库存类:

public void Scanitem(String item, String trueouputext, String textifconditionisnotmet) {

        if (inv.contains(item) == true) {
            System.out.println(trueouputext);

        } else if (inv.contains(item) == false) {
            System.out.println(textifconditionisnotmet);
        }

public static ArrayList<String> inv = new ArrayList<String>();

各位有什么推荐吗?

标签: java

解决方案


是否有其他步骤来填充库存(变量inv)?

此外,您是否不希望 ScanItem 根据是否找到该项目来回答真假?然后你会有这样的事情:

public boolean scanitem(String item) {
    return ( inv.contains(item) );
}

if ( p.scanItem(flyingBroom) ) {
    System.out.println("Good choice! You managed to kill the Hungarian Horntail dragon and to get the golden egg");
} else {
    System.out.println("You dont have the broom. Try to search for the broom");
}

这会让你更接近你想要的。但是,您还需要在代码中添加另外两个问题:

您将需要某种循环,而不是challengeDragon从自身内部调用。

不知何故,必须使用 scanItem 的返回值来决定是否循环。


推荐阅读