修复if if if语句为java游戏

时间:2014-09-14 14:22:42

标签: java if-statement

String option = (String) JOptionPane.showInputDialog(frame, 
    "A wooden crate appears! It looks ready to kick your butt!",
    "Battle!",
    JOptionPane.QUESTION_MESSAGE, 
    null, 
    attacks, 
    attacks[0]);
    if (option.equals("slash"))
    {
        damage = (int)(Math.random()*(stick.getMax()-stick.getMin()) + stick.getMin());
    }               
    else if (option.equals("magic"))
    {
        if (playerMana >= 25)
        { 
            damage = (int) intellect*((stick.getMax()-stick.getMin())/2) + (int)Math.round(Math.pow(playerLevel, 1.20));
            playerMana -= 25;
        }
        else
        {
            damage = 0;
        }
    }
    else if (option.equals("run"))
    {
        out.println("fail! you run from the fight!");
    }
    else if (option.equals("healing") && healPots >= 1)
    {
        playerHp+= (playerHp*0.15);
    }
    else
    {
        out.println("you have no potions! Get some in town to heal your hp!");
    }

Stick只是我用最小最大伤害值制造的武器对象。玩家hp和法力值均为100. healPots为0。

我正在尝试制作一个RPG风格的战斗系统,玩家可以选择并轮流使用。但是,无论选择什么,循环都会自动跳到最终的其他位置。

1 个答案:

答案 0 :(得分:1)

当您在SO上提出问题时,提供最小的运行示例总是一个好主意。

我从您的代码中移除了实际游戏逻辑,并将frame替换为null

public class ShubhankarsQuestion
{
    public static void main(String[] args)
    {
        String[] attacks = {"slash", "magic", "run", "healing"};

        String option = (String) JOptionPane.showInputDialog(null,
                "A wooden crate appears! It looks ready to kick your butt!",
                "Battle!", JOptionPane.QUESTION_MESSAGE,
                null, attacks, attacks[0]);

        if (option.equals("slash"))
        {
            System.out.println("You chose slash...");
        }
        else if (option.equals("magic"))
        {
            System.out.println("You chose magic...");
        }
        else if (option.equals("run"))
        {
            System.out.println("You chose run...");
        }
        else if (option.equals("healing"))
        {
            System.out.println("You chose healing...");
        }
        else
        {
            System.out.println("you have no potions! Get some in town to heal your hp!");
        }
    }
}

这完美无缺。要么您没有向我们展示实际代码,要么您的问题出在其他地方。

  

你能告诉我们变量攻击的定义吗?

String[] attacks = {"slash!", "magic!", "healing!", "run!"};

嗯,你有它。您将"slash!""slash"进行比较,这当然不相等:)

从数组中删除感叹号,或将它们添加到比较的字符串中。