我正在使用Java开发一个项目,我编写了一个if语句,以便根据用户输入的内容调用不同的构造函数(无论是要加载默认文件还是选择自己的文件路径)。但是它似乎只是去了else语句而不是if语句。我试着在调用构造函数后继续,但是我收到错误。
import java.io.File;
import java.util.Scanner;
public class ShortenerUtility {
public static void main(String[] args)
{
System.out.println("Would you like to open the default file?");
Scanner sc1 = new Scanner(System.in);
String input1;
input1 = sc1.nextLine().toLowerCase();
System.out.println(input1);
if (input1 == "yes")
{
Shortener s1 = new Shortener();
}
else
{
System.out.println("Please Enter the file path");
Scanner sc2 = new Scanner(System.in);
String input2;
input2 = sc1.nextLine();
Shortener s2 = new Shortener(input2);
}
}
}
返回以下内容:
Would you like to open the default file?
Yes
yes
Please Enter the file path
当我在Shortener之后放下继续时,我得到了这个:
Shortener s1 = new Shortener();
continue;
出现此错误消息:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
continue cannot be used outside of a loop
at ShortenerUtility.main(ShortenerUtility.java:20)
答案 0 :(得分:0)
比较字符串时,您需要使用.equals()
代替==
,如下所示:
if (input1.equals("yes")) { ... }