这是我第一次使用这个论坛而且我对整个Java体验都不熟悉,所以如果这是一个非常简单的修复,请原谅我。
我正在尝试为学校制作这个项目,但我无法让我的if else声明工作。 正如你所看到的,如果你进入近战或远程,一切都没问题,但如果不这样做,它会重定向你。 我的问题是,即使你输入近战或远程,它也会先引导你进入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.");
}
}
}
答案 0 :(得分:6)
您的代码:
if(a) ...
if(b) ... else ...
所以在每种情况下(a
true
或false
)如果b
为false
,它将进入else
声明(至更具体地说,如果Type
不等于ranged
,则else
部分将被执行。
我认为你想要的是
if(a) ... else if(b) ... else ...
使用您的代码:
if (Type.equalsIgnoreCase("melee")) {
} else if (Type.equalsIgnoreCase("ranged")) {
} else {
}
答案 1 :(得分:5)
您需要使用if
,else if
,else
构造。
if (Type.equalsIgnoreCase("melee")) {
// ...
}
else if (Type.equalsIgnoreCase("ranged")) {
// ...
}
else {
// ...
}
答案 2 :(得分: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.");
}
答案 3 :(得分: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..
}
}
}
为你工作?