import javax.swing.JOptionPane;
public class Cortana2 {
public static void main(String[] args) throws Exception {
//Declaring Variables (Add more commands)
String command;
// Command will always stay the same
// All strings below are commands to put in
String Steam;
String League;
League=("League");
Steam=("Steam");
command= JOptionPane.showInputDialog("Give a valid command");
if (command == null) {
JOptionPane.showMessageDialog(null, "This is not a valid command. If you have forgotten what commands are valid, please refer to Devon for assistance");
JOptionPane.getRootFrame().dispose();
} else if (command == League) {
Runtime.getRuntime().exec("\"D:/LeagueClient.exe\"");
} else if (command == Steam) {
Runtime.getRuntime().exec("\"C:/Program Files (x86)/Steam/Steam.exe\"");
}
}
}
不能 100% 确定为什么会出现错误。我看到其他人说要从“if”语句中删除分号,但当我运行程序并输入命令时,什么也不会执行。抱歉,如果有任何内容格式不正确。
最佳答案
if (command == null);
不要加“;”在 if/else 语句的末尾。
but then nothing executes when I run the program and type in commands
不要使用 ==
来比较字符串。
改用String.equals(...)
方法
此外,变量名称不应以大写字符开头。
关于java - 收到 'else',但没有 'if' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40570596/