(对不起,我奇怪的冠军,但我不能找出真正的问题是什么)获取字符串→分割→字符串的if/else if语句返回else语句没有理由
下面的代码应该首先从命令行获得一个字符串(工作方式),然后输入被分割(完美地工作;我通过在if/else之前打印两个字符串来检查,如你在我注释到的部分中看到的那样)和那么它应该检查分割字符串的第一部分是什么。例如,如果它等于“tweet”,它应该使用Tweet方法进行处理。
但不知何故,它没有得到正确的。它总是执行else语句...
Scanner sc = new Scanner(System.in);
System.out.print("> ");
String input = sc.nextLine();
String[] splitString = input.split(" ");
if(splitString.length != 2){ throw new IllegalArgumentException(); }
String command = splitString[0];
String value = splitString[1];
/*System.out.print(command);
System.out.print(value);*/
if(command == "tweet") { Tweet(value); }
else if(command == "help") { ShowHelp(); }
else { System.out.println("Command "+command+" not found."); }
我试图进入 “鸣叫ASDF”,但它返回
> tweet asdf
Command tweet not found.
我做了什么错?我很困惑D:
它的工作原理!谢谢!! :D – bertcc423
没问题。如果这有帮助,请接受答案。 –