我想问用户他是否想要创建一个名为file.elt
的文件。我正在尝试使用Scanner
类的switch语句来完成此操作。使用扫描仪时无限循环 - java
这里是我的代码:
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();
的sc
对象在程序中,在那里我要求的文件的名称的开头声明。
的sc
声明:
String file;
Scanner sc = new Scanner(System.in);
System.out.println("Type the name of the file .dat .");
file=sc.nextLine();
的问题是,while循环是无限的,我不知道为什么。
也改变'sc.hasNext()''到sc.hasNextLine()' – TheLostMind 2014-09-24 10:20:14
太好了!谢谢! – daro 2014-09-24 10:22:19