[英]if else statement not working properly
this is my first time using this forum and I am new to the whole Java experience, so forgive me if this is a really simple fix. 这是我第一次使用该论坛,并且对Java的整体经验还很陌生,因此,如果这真的很简单,请原谅我。
I am trying to make this project for school, but I can't get my if else statement to work. 我正在尝试使这个项目上学,但是我无法得到我的if if陈述。 As you can see, if you enter melee or ranged everything is alright, but if you don't it redirects you.
如您所见,如果您输入近战或远程攻击,一切都很好,但是如果您不输入,它将重定向您。 My problem is that even if you type melee or ranged, it will first direct you to the if method, and after that it will immediately redirect you to the else statement.
我的问题是,即使您输入近战或范围,它也会首先将您定向到if方法,然后它将立即将您重定向到else语句。
Does anyone know how I can fix this? 有谁知道我该如何解决?
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.");
}
}
}
Your code: 您的代码:
if(a) ...
if(b) ... else ...
So in every cases ( a
true
or false
) if b
is false
it will goes into the else
statement (to be more specific, if the Type
is not equal to ranged
, the else
part will be executed). 因此,在每一个案件(
a
true
或false
),如果b
是false
它将进入else
声明(更具体的,如果Type
不等于ranged
,在else
部分将被执行)。
I think what you want is 我想你想要的是
if(a) ... else if(b) ... else ...
Using your code: 使用您的代码:
if (Type.equalsIgnoreCase("melee")) {
} else if (Type.equalsIgnoreCase("ranged")) {
} else {
}
You need to use an if
, else if
, else
construct. 您需要使用
if
, else if
, else
结构。
if (Type.equalsIgnoreCase("melee")) {
// ...
}
else if (Type.equalsIgnoreCase("ranged")) {
// ...
}
else {
// ...
}
Do it like this 像这样做
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.");
}
Does 是否
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..
}
}
}
work for you? 为你工作?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.