java - 收到 'else',但没有 'if' 错误

标签 java

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/

相关文章:

java - 如何对 Stream 的某些部分执行归约操作

java - 如何提示用户再次运行程序?

java - 如何使用链表在java中实现已经很好实现的模块(如堆栈)

java - Java 日历中的时区

java - 如何在回收站 View 中隐藏广告 View

java - 在 jax-ws Web 服务中子类化 ObjectFactory 的问题

java - 使用正则表达式从字符串中删除一些额外的文本

java - 反转堆栈并添加到 ArrayList 的最有效方法

java - 在 Java 中选择 ArrayList 中最后 25% 的对象

java - 在 JBoss DB 源配置中获取 "ORA-12514, TNS:listener does not currently know of service requested in connect descriptor"