我是一名学习者(目前非常基础)。当找不到合法答案时,我会提出问题。
我可以在公共(public)类中声明扫描仪,这样我就不需要在我使用的每个函数中声明吗?
public class name
{
//can i declare scanner here? and how?
public static int xxx2(int kk)
{
Scanner kb=new Scanner( System.in);
//code
return kk;
}
public static int xxx1(int kk)
{
Scanner kb=new Scanner(System.in);
//code
}
}
最佳答案
是的,您可以只保留Scanner
作为记录。由于您使用的是 System.in
,因此您只需内联初始化它,而不必担心关闭它:
public class MyClass {
private static Scanner myScanner = new Scanner(System.in);
// Methods that can use myScanner
}
关于java - 我可以在公共(public)类(class)中声明扫描仪吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52829572/