This question already has an answer here:
这个问题在这里已有答案:
- Why can't I do assignment outside a method? 7 answers
为什么我不能在方法之外做任务? 7个答案
The code is the following:
代码如下:
import java.util.Scanner;
public class Programm2 {
int n;
Scanner inp = new Scanner(System.in);
n = inp.nextInt();
}
All that I'm trying to get is user input and I don't know why get this error on to the inp's declaration line. My IDE is Eclipse and the class is created with the default settings(nothing new added to it).
我想要得到的只是用户输入,我不知道为什么会在inp的声明行中出现此错误。我的IDE是Eclipse,并且使用默认设置创建了类(没有添加任何新内容)。
1 个解决方案
#1
1
Your code needs to go inside a method.
您的代码需要进入方法。
class Programm2
{
int n = 0;
public void aMethod() {
Scanner inp = new Scanner(System.in);
n = inp.nextInt();
}
}
#1
1
Your code needs to go inside a method.
您的代码需要进入方法。
class Programm2
{
int n = 0;
public void aMethod() {
Scanner inp = new Scanner(System.in);
n = inp.nextInt();
}
}