令牌“;”上的语法错误,在Scanner对象上预期[重复]

时间:2022-08-11 22:31:16

This question already has an answer here:

这个问题在这里已有答案:

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(); 
   }
}