有没有办法从Scanner中获取输入,而不需要声明它?

huangapple go评论79阅读模式
英文:

Is there any way to get input from a Scanner without declaring it?

问题

通常情况下,您会使用 Scanner scan = new Scanner(System.in); 来声明一个输入 Scanner。

是否有任何方法可以在不创建该类的对象的情况下使用 Scanner 中的 nextInt()nextLine() 等方法?

英文:

Usually you would declare an input Scanner with Scanner scan = new Scanner(System.in);

Is there any way to use a method in the Scanner such as nextInt() or nextLine() without making an object of the class?

答案1

得分: 1

你可以创建一个实用工具类,为该实用工具类创建静态方法,并在使用import static来导入实用工具类中的方法。静态方法导入允许您在不必显式使用类的情况下使用类中的方法。参见这里的引用 1

文件 TestApp.java

package com.example;

import static com.example.utils.ConsoleReader.nextInt;
// 或者使用 "import static com.example.utils.ConsoleReader.*;" 如果你想导入类 "ConsoleReader" 中的所有静态方法

public class TestApp {

	public static void main(String[] args) {
		System.out.println("输入一个数字");
        // 以下代码行调用类 "ConsoleReader" 中的方法,无需显式引用该类。
		int input = nextInt();
		System.out.println("你输入了 " + input);
	}

}

文件 ConsoleReader.java

package com.example.utils;

import java.util.Scanner;

public class ConsoleReader{
	private static Scanner scan = new Scanner(System.in);
	
	public static int nextInt() {
		return scan.nextInt();
	}

    public static String nextLine() {
		return scan.nextLine();
	}
}

英文:

You could create a utility class, create static methods in that utility class and use a import static for methods in the utility class. Static method import allows you to use methods from a class without having to explicitly use the class. See the reference here.

File TestApp.java


package com.example;

import static com.example.utils.ConsoleReader.nextInt;
// Or use "import static com.example.utils.ConsoleReader.*;" if you want to import every static methods in class "ConsoleReader"

public class TestApp {

	public static void main(String[] args) {
		System.out.println("Enter the number the number");
        // The following line calls the method of the class "ConsoleReader" without having to reference it.
		int input = nextInt();
		System.out.println("You have entered " + input);
	}

}


File ConsoleReader.java

package com.example.utils;

import java.util.Scanner;

public class ConsoleReader{
	private static Scanner scan = new Scanner(System.in);
	
	public static int nextInt() {
		return scan.nextInt();
	}

    public static String nextLine() {
		return scan.nextLine();
	}
}

答案2

得分: 0

你可以创建一个返回所需变量类型的静态类。

例如,如果你想为读取下一个整数创建自己的类:

public static int nextInt(){
    Scanner scanner = new Scanner(System.in);
    return scanner.nextInt();
}

然而,在实际情况下,你仍然需要声明它。

然后,你只需在你的代码中使用 nextInt() 来获取下一个整数。

英文:

You could create a static class which returns the desired variable type.

For example, if you wanted to create your own class for reading the next integer:

public static int nextInt(){
    Scanner scanner = new Scanner(System.in);
    return scanner.nextInt();
}

However in reality you still have to declare it.

Then you can simply put nextInt() in your code to get the next Integer.

huangapple
  • 本文由 发表于 2020年9月17日 07:01:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63929022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定