if else语句不能正常工作

San*_*non -1 java if-statement

这是我第一次使用这个论坛而且我对整个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.");

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*nGa 6

你的代码:

if(a) ...
if(b) ... else ...
Run Code Online (Sandbox Code Playgroud)

因此,在每一个案件(a truefalse)如果bfalse它会进入else声明(更具体的,如果Type不等于ranged,该else部分将被执行).

我想你想要的是什么

if(a) ... else if(b) ... else ...
Run Code Online (Sandbox Code Playgroud)

使用你的代码:

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

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

} else {

}
Run Code Online (Sandbox Code Playgroud)


Dun*_*nes 5

您需要使用的if,else if,else施工.

if (Type.equalsIgnoreCase("melee")) {
  // ...
}
else if (Type.equalsIgnoreCase("ranged")) {
  // ...
}
else {
  // ...
}
Run Code Online (Sandbox Code Playgroud)