if 语句中的 if 语句未运行

时间:2021-03-19 02:12:10

标签: java if-statement

所以我正在尝试制作一个基于文本的 rpg 游戏,这里应该发生的事情是当您通过输入hunt 来激活狩猎时,它会询问您是或否。然而,即使我输入是,你杀死了野猪的消息也不会弹出。我对 Java 还很陌生,如果代码不好,我很抱歉。

//Branches
            if (ins.equals("branches") && loc.equals("forest")) {
                System.out.println("You got a branch");
                ib++;
            } else if (ins.equals("branches") && !"forest".equals(loc)) {
                System.out.println("You need to be in the forest to cut branches");
            } else if (ins.equals("bcount")) {
                System.out.println(ib);
            }
            //Branches


            //Stones
            if (ins.equals("stones") && loc.equals("mountains")) {
                System.out.println("You got a stone");
                is++;
            } else if (ins.equals("stones") && !"mountains".equals(loc)) {
                System.out.println("You need to be in the mountains to gather stones");
            } else if (ins.equals("scount")) {
                System.out.println(is);
            }
            //Stones


            //Spears
            if (ins.equals("spear") && is >= 1 && ib >= 1) {
                System.out.println("+1 spear");
                is--;
                ib--;
                ispear++;
            } else if (ins.equals("spear") && is < 1) {
                System.out.println("Insufficient resources");
            } else if (ins.equals("spear") && ib < 1) {
                System.out.println("Insufficient resources");
            } else if (ins.equals("spearcount")) {
                System.out.println(ispear);
            }
            //Spears


            //Hunt
            if (ins.equals("hunt") && ispear >= 1) {

                System.out.println("A wild boar comes charging at you! Throw spear? (yes or no)");
                


                if (ins.equals("yes")) {

                    System.out.println("You killed the boar!");

                }


            } else if (ins.equals("hunt") && ispear < 1) {

                System.out.println("You dont have any spears");

            }

        }

1 个答案:

答案 0 :(得分:0)

如果顶部的 while 循环控制输入,则代码不可能返回到内部 if 语句,因为 ins 不能同时等于“hunt”和“yes”。您需要按照@Spectric 在评论中的建议进行操作并再次阅读输入,或者您需要使用其他方式。

在下面的例子中,我们使用了一个布尔值作为标志 Boolean hunting = false; 和一个 OR 条件 || 来让我们回到狩猎部分 if (huting == true || ins.equals("hunt")....) 然后最后我们需要确保我们在狩猎开始/结束时触发标志打开/关闭 hunting = true;hunting = false;

然后全部放在一起:

//Flag that allows us to trigger hunting
//This needs to be placed outside of the while loop
Boolean hunting = false;

//While loop that gets input and prints the next instruction
while (ins != null)
{
    //Your code here to get the input
    //...
    //ins = scanner.nextLine();
    //...
    
    //Hunt
    //add an or "||" condition so that if the hunting flag is true then we can get back inside this if statement:
    if (hunting == true || ins.equals("hunt") && ispear >= 1)
    {
        //Set the hunting flag to true, so that we can get back to here in the next loop:
        hunting = true;
        System.out.println("A wild boar comes charging at you! Throw spear? (yes or no)");
        if (ins.equals("yes"))
        {
            System.out.println("You killed the boar!");

            //Reset the hunting flag so future commands don't break:
            hunting = false;
        }
        //Make sure you have an else statement to set the hunting flag back to false
        else
        {
            System.out.println("The boar ran away!");

            //Reset the hunting flag so future commands don't break:
            hunting = false;
        }
    }
    else if (ins.equals("hunt") && ispear < 1)
    {
        System.out.println("You dont have any spears");
    }
}