我最近遇到了这个令人沮丧的问题,扫描程序完全忽略了System.in输入流中的一些输入.这是一个例子:
Do you want a cookie? Yes or no.
Run Code Online (Sandbox Code Playgroud)
现在,我输入是:
yes
Run Code Online (Sandbox Code Playgroud)
结果:
Cookie for you!
Run Code Online (Sandbox Code Playgroud)
现在,如果我说不:
no
no
Run Code Online (Sandbox Code Playgroud)
结果:
No Cookie for you.
Run Code Online (Sandbox Code Playgroud)
得到它?如果我说是,那就接受它.如果我说不,我必须输入2次.
如果你真的需要一些代码.这里有一些:)
public static void main(String[] paramArgs){
MainEW sMain = new MainEW();
Scanner s = sMain.scanner;
System.out.println("Enter a file path.");
System.out.println("Example: /Users/Some_User/Desktop/Some_Folder");
String defPath = s.next();
System.out.println("Enter a name for the file.");
String defName = s.next() + ".txt";
System.out.println("Now, enter what you want to write to the file.");
s.nextLine();
String defText = s.nextLine();
System.out.println("Do you want to …Run Code Online (Sandbox Code Playgroud)