我想询问用户是否要创建一个名为file.elt或不是的文件.我正在尝试使用Scannerclass 使用switch语句.
这是我的代码:
System.out.println("Do you want to create the file.elt? (Y/N)");
strOption=sc.nextLine();
OUTER:
while (sc.hasNext()) {
switch (strOption) {
case "Y":
case "y":
elements.createElements(file);
break OUTER;
case "N":
case "n":
System.out.println("There will be no file.elt created! .");
break OUTER;
default:
System.out.println("Please, type Y or N.");
strOption=sc.nextLine();
break;
}
}
sc.close();
Run Code Online (Sandbox Code Playgroud)
该sc对象在程序开头声明,我要求提供文件名.
该sc声明是:
String file;
Scanner sc = new Scanner(System.in);
System.out.println("Type the name of the file .dat .");
file=sc.nextLine();
Run Code Online (Sandbox Code Playgroud)
问题是while循环是无限的,我不知道为什么.