接收“其他”'没有'如果'错误

时间:2016-11-13 04:33:44

标签: 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%肯定我为什么会收到错误。我已经看到其他人说要删除分号,如果'语句但是当我运行程序并输入命令时没有执行任何操作。对不起,如果有什么东西看起来格式不好。

3 个答案:

答案 0 :(得分:3)

if (command == null);

不要放一个&#34 ;;"在if / else语句的末尾。

  

但是当我运行程序并输入命令

时没有执行任何操作

请勿使用==来比较字符串。

而是使用String.equals(...)方法

变量名也不应以大写字母开头。

答案 1 :(得分:1)

每次测试结束时都有一个无关的分号

else if (command == League); // <- remove these semicolons 

使用==来比较字符串也会让你感到悲伤。请改用.equals()

答案 2 :(得分:0)

从If中删除分号,否则如果使用等于方法,则代码看起来像这样..

  command= JOptionPane.showInputDialog("Give a valid command")

       {
                 if (command.equals(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.equals(League))
                 {   
                 Runtime.getRuntime().exec("\"D:/LeagueClient.exe\"");
                 }else if (command.equals(Steam))
                 {  
                 Runtime.getRuntime().exec("\"C:/Program Files (x86)/Steam/Steam.exe\"");  
                 }   

        System.exit(0);
       }