Java Scanner
初识Java,想写个程序使用Scanner进行输入。
这是一开始写的:
//创建一个扫描器对象,用于接收键盘数据
Scanner scan = new Scanner(Scanner.in);//这里,Scanner.in错了。应该是System.in。
//这也能错,我是傻逼。
System.out.println("使用next方式接收:");
//使用next方式接收
String str = Scanner.nextLine();
System.out.println("输出的内容为:"+str);
//及时关闭IO流的类,否则会一直占用资源
Scanner.close();
无法运行。
有两个情况,这是第一个:
Non-static method 'nextLine()' cannot be referenced from a static context
这是第二个:
Cannot resolve symbol 'in'
后来我看了看别人的代码,改了改自己的代码:
//创建一个扫描器对象,用于接收键盘数据
Scanner scan = new Scanner(System.in);//这里,Scanner.in错了。应该是System.in。
//这也能错,我是傻逼。
System.out.println("使用next方式接收:");
//使用next方式接收
String str = scan.nextLine();//还有,这里需要用新建的对象名。
//这也能错,我是真的傻逼到家了。
System.out.println("输出的内容为:"+str);
//及时关闭IO流的类,否则会一直占用资源
Scanner.close();
这一次成功跑通。
参考:
Java中Scanner详细用法
JAVA中“Cannot make a static reference to the non-static method nextLine() from the type Scanner”的解决方法