-1

这是我第一次使用这个论坛,我对整个 Java 体验还是新手,如果这是一个非常简单的修复,请原谅我。

我正在尝试为学校制作这个项目,但我的 if else 语句无法正常工作。如您所见,如果您进入近战或远程,一切都很好,但如果您不这样做,它会重定向您。我的问题是,即使您键入 melee 或 ranged,它也会首先将您定向到 if 方法,然后立即将您重定向到 else 语句。

有谁知道我该如何解决这个问题?

newchamp.Type = JOptionPane.showInputDialog(null, "What type of champion have you summoned? (melee or ranged)", "Type", JOptionPane.PLAIN_MESSAGE);
        if (Type.equalsIgnoreCase("melee")) {
           JOptionPane.showMessageDialog(null,"You have now confirmed your champion, you can not edit anything from this point on.");
        }
        if (Type.equalsIgnoreCase("ranged")) {
              JOptionPane.showMessageDialog(null,"You have now confirmed your champion, you can not edit anything from this point on.");
        }
        else {
            JOptionPane.showMessageDialog(null,"You can only choose Melee or Ranged!");
            newchamp.Type = JOptionPane.showInputDialog(null, "What type of champion have you summoned? (Melee or Ranged)", "Type", JOptionPane.PLAIN_MESSAGE);
               JOptionPane.showMessageDialog(null,"You have now confirmed your champion, you can not edit anything from this point on.");

        }
    }
}
4

4 回答 4

6

你的代码:

if(a) ...
if(b) ... else ...

因此,在每种情况下(a truefalse) if bisfalse它都会进入else语句(更具体地说,如果Type不等于ranged,则将执行该else部分)。

我想你想要的是

if(a) ... else if(b) ... else ...

使用您的代码:

if (Type.equalsIgnoreCase("melee")) {

} else if (Type.equalsIgnoreCase("ranged")) {

} else {

}
于 2013-02-11T08:14:01.980 回答
5

您需要使用if, else if,else构造。

if (Type.equalsIgnoreCase("melee")) {
  // ...
}
else if (Type.equalsIgnoreCase("ranged")) {
  // ...
}
else {
  // ...
}
于 2013-02-11T08:12:20.510 回答
1

像这样做

String   type = JOptionPane.showInputDialog(null, "What type of champion have you summoned? (melee or ranged)", "Type", JOptionPane.PLAIN_MESSAGE);
            if (Type.equalsIgnoreCase("melee")) {
               JOptionPane.showMessageDialog(null,"You have now confirmed your champion, you can not edit anything from this point on.");
            }else if (type.equalsIgnoreCase("ranged")) {
                  JOptionPane.showMessageDialog(null,"You have now confirmed your champion, you can not edit anything from this point on.");
            }
            else {
                JOptionPane.showMessageDialog(null,"You can only choose Melee or Ranged!");
               type = JOptionPane.showInputDialog(null, "What type of champion have you summoned? (Melee or Ranged)", "Type", JOptionPane.PLAIN_MESSAGE);
                   JOptionPane.showMessageDialog(null,"You have now confirmed your champion, you can not edit anything from this point on.");
            }
于 2013-02-11T08:15:51.670 回答
1

newchamp.Type = JOptionPane.showInputDialog(null, "What type of champion have you summoned? (melee or ranged)", "Type", JOptionPane.PLAIN_MESSAGE);
    if (Type.equalsIgnoreCase("melee")) {
       ..something..
    }
    else if (Type.equalsIgnoreCase("ranged")) {
          ..something..
    }
    else {
       ..something..
    }
}
}

为你工作?

于 2013-02-11T08:17:21.920 回答