【问题标题】:Calling scanner parameter调用扫描仪参数
【发布时间】:2018-12-10 21:59:28
【问题描述】:

我创建了两个类,MethodActionClass。在Mehod 类中,我使用Scanner 参数创建了passingParameters 方法,在ActionClass 中,我创建了一个对象并调用passingParameters 方法,如下所示。但我不知道如何调用Scanner 参数。看起来调用Scanner 和其他参数之间存在差异。还是我错了?我如何在ActionClass 中调用扫描仪?

 public class Method{

 public void passingParameters(Scanner input){
    int numInput=input.nextInt();
    System.out.println(numInput);
}
}


 public class ActionClass {

 public static void main(String[] args) {
    Method newObject=new Method(how to call Scanner? );
    newObject.passingParameters();
}

【问题讨论】:

  • Method newObject=new Method(); Scanner scanner = new Scanner(System.in); newObject.passingParameters(scanner);

标签: java parameter-passing java.util.scanner


【解决方案1】:

可能是这样的:

如果您想使用您在 ActionClass 类的示例 ma​​in() 方法中指出的 Scanner 对象,那么您需要在接受 Scanner 对象的方法类:

package whateverpackage;

import java.util.Scanner;

public class Method {

    // Declare a Scanner Object field.
    // This will make the Object global
    // to the entire Class.
    private Scanner scannerInput;

    // Class Constructor
    public Method (Scanner input) {
        this.scannerInput = input;    
    }

    public void passingParameters(){
        System.out.println("Enter a integer value for Parameter: ");
        int numInput = scannerInput.nextInt();
        input.nextLine(); // Empty scanner buffer
        System.out.println("Supplied value was: " + numInput);
    }
}

然后是你的 ActionClass

package whateverpackage;

import java.util.Scanner;

public class ActionClass {

    public static void main(String[] args) {
        Method newObject = new Method(new Scanner(System.in));
        newObject.passingParameters();
    }

}

如果您打算将 Method 类中的 Scanner 对象广泛用于各种方法,这将特别方便。但是,如果您只打算在 Method 类中偶尔使用 Scanner 对象,那么我认为 @user7 在评论中描述的方式可能是进入的方式在这种情况下不需要构造函数来执行任务:

package whateverpackage;

import java.util.Scanner;

public class Method {

    // Method accepts a Scanner Object as an argument.
    public void passingParameters(Scanner input){
        System.out.println("Enter a integer value for Parameter: ");
        int numInput = input.nextInt();
        input.nextLine(); // Empty scanner buffer
        System.out.println("Supplied value was: " + numInput);
    }
}

然后是你的 ActionClass

package whateverpackage;

import java.util.Scanner;

public class ActionClass {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Method newObject = new Method();
        newObject.passingParameters(scanner);
    }

}

Scanner 对象也可以声明为 Class 字段,并且在 ActionClass 类中是全局的,然后可以从该类中的任何方法中使用,而无需总是声明 Scanner 的新实例。然而,由于 ma​​in() 方法是静态方法,因此需要将 Object 声明为 staticActionClass 类中使用此 Scanner 对象的任何方法也需要声明为静态。在这种情况下没什么大不了的,但总会有一段时间(here's some interesting reading about static)。为了解决这个问题,不要在 main() 方法中使用对象,完全使用不同的方法:

package whateverpackage;

import java.util.Scanner;

public class ActionClass {

    private Scanner scanner = new Scanner(System.in);

    // *****************************************
    public static void main(String[] args) {
        new ActionClass().startApp(args);
    }
    // *****************************************

    private void startApp(String[] args) {
        Method newObject = new Method();
        newObject.passingParameters(scanner);

        justAnotherMethod();
    }

    private void justAnotherMethod () {
        System.out.println("What's your name: ");
        String name = scanner.nextLine();
        System.out.println("Your Name is: " + name);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多