文字游戏如果声明麻烦

时间:2014-06-14 22:43:43

标签: java

我正在制作一个文本游戏而且我遇到了一个破坏游戏的错误,或者可能是因为我只是用它来学习一些我可能正在做错的java这里是我被困在哪里我认识的部分bug是。

意味着它将决定用户拥有剑或盔甲的天气,因此根据它改变选项但它只运行第一个打印命令,除非我手动设置hasSword或hasArmour变量然后它将一直运行。

static void shop()
{
    System.out.println("As you enter the shop you check how much gold you have left. As you count it you find you have " + gold + " gold to spend this time!");

    if(hasSword && hasArmour == true)
    {
        System.out.println("Looks like the shop is all bought out!!\n1. Back");

        in = input.nextInt();

        if(in == 1)
        {
            city();
        }
    }
    else if(hasSword == true)
    {
        System.out.println("1. Armour 50 gold");
        System.out.println("2. Exit");

        in = input.nextInt();

        if(in == 1)
        {
            if(gold >= 50)
            {
                hasArmour = true;
                hp = 20;
                gold = gold - 50;
                city();
            }
            else if(gold < 50)
            {
                System.out.println("Sorry you do not have enough gold");
                shop();
            }
        }
        else if(in == 2)
        {
            city();
        }
    }
    else if(hasArmour == true)
    {
        System.out.println("1. Sword 10 gold");
        System.out.println("2. Exit");

        in = input.nextInt();

        if(in == 1)
        {
            if(gold >= 10)
            {
                hasSword = true;
                dmg = 10;
                gold = gold - 10;
                city();
            }
            else if(gold <10)
            {
                System.out.println("Sorry you do not have enough gold");
                shop();
            }
        }
        else if(in == 2)
        {
            city();
        }
    }
    else if(hasSword && hasArmour == false)
    {
        System.out.println("1. Sword 10 gold");
        System.out.println("2. Armour 50 gold");
        System.out.println("3. Back");

        in = input.nextInt();

        if(in == 1)
        {
            if(gold >= 10)
            {
                hasSword = true;
                dmg = 10;
                gold = gold - 10;
                city();
            }
            else if(gold <10)
            {
                System.out.println("Sorry you do not have enough gold");
                shop();
            }
        }
        else if(in == 2)
        {
            if(gold >= 50)
            {
                hasArmour = true;
                hp = 20;
                gold = gold - 50;
                city();
            }
            else if(gold < 50)
            {
                System.out.println("Sorry you do not have enough gold");
                shop();
            }
        }
        else if(in == 3)
        {
            city();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

默认情况下,hasArmorhasSword默认为false,如果您未明确声明它们。 问题出在你最后的else if条件中。 你写了

else if(hasSword && hasArmour == false)

由于==的优先级高于&&,因此此条件被解释为

(hasSword && (hasArmour == false))

并且由于Java中的短路机制,只要条件的第一部分(即hasSword)为假,整个条件就被认为是假并且你的块没有被执行。

要执行块,你必须这样写:

else if((hasSword && hasArmour) == false)

现在首先评估逻辑运算符,然后检查它是否为false,并且执行了块。

为了防止与条件混淆,您可以稍微改变代码的结构。但最终,这将提高可读性,而行为将保持不变。

if(hasSword) {
    if(hasArmor) {
        // Your first block
    }
    else {
        // Your second block
    }
}
else {
    if(hasArmor) {
        // Your third block
    }
    else {
       // Your fourth block
    }
}