简体   繁体   English

仅从主要方法使用扫描仪

[英]Use scanner only from main method

I have this class (with setters, getters and one method) that asks from a user a number indefinitely until he types -1. 我有一个此类(带有setter,getter和一个方法),该类向用户无限地询问一个数字,直到他键入-1。 I've called the Scanner Method from both, the main method and the class itself, is there a way to call the Scanner method only once only from the main method and apply the input to the class every time it is needed? 我已经从main方法和类本身两者中调用了Scanner方法,是否有一种方法只能从main方法中调用一次Scanner方法,并在需要时将输入应用于类? I really appreciate your help. 非常感谢您的帮助。 If something is not clear, please contact me. 如果不清楚,请与我联系。

Here's the Class Code: 这是课程代码:

public class calculation {

int current = 0;
int maxNum = 0;
int minNum;
int counter=0;
float sum = 0;
float avg;

Scanner scan = new Scanner(System.in);


public void setMin(int min){
    this.minNum = min;
}

public int getMin(){
    return minNum;
}

public void setMax(int max){
    this.maxNum = max;
}

public void setSum(float sum){
    this.sum += sum;
}

public void minMax(int current){
    setMin(current);
    while(current!=-1){
        setSum(current);;
        if(current>getMin()){
            setMax(current);
        }else if(current<getMin()){
            setMin(current);;
        }

        current = scan.nextInt();

        counter++;
    }

    System.out.println("The smallest number you entered was: \n" + minNum);
    System.out.println("The biggest number you entered was: \n" + maxNum);
    System.out.println("The sum of all those numbers is: \n" + sum);
    System.out.println("The avarege number is: \n" + (sum/counter));

}

}

And here's the main method code: 这是主要的方法代码:

public class minusOne {

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    calculation cal1 = new calculation();

    System.out.println("Type numbers at will, when finish, type -1 and press enter");
    cal1.minMax(scan.nextInt());

    scan.close();

}

}

From what I understand, you don't want to have two call to new Scanner(System.in); 据我了解,您不希望两次致电new Scanner(System.in);

To avoid this, you can simply, in your class calculation, write : Scanner scan; 为了避免这种情况,您可以在类计算中简单地编写: Scanner scan;

And add a constructor : 并添加一个构造函数:

public calculation(Scanner sc){
    scan = sc;
}

Of course, in the main method you should write : new calculation(scan) 当然,在主要方法中,您应该编写: new calculation(scan)

I hope I answered your question 我希望我回答了你的问题

Note: in Java your classes name should start with uppercase letter, it should be Calculation 注意:在Java中,您的类名称应以大写字母开头,应为Calculation

You have some alternatives for this, you can have your Calculator class with a constructor that takes a Scanner as a parameter and then store it in a field, or you cand have a public field in the Calculator class and in your main when you get the scanner just affect this field (but it should be private, you can change it via getters and setters methods). 您可以选择其他方法,可以让您的Calculator类带有一个构造函数,该构造函数将Scanner作为参数,然后将其存储在字段中,或者在获得扫描程序只会影响此字段(但应为私有字段,您可以通过getter和setter方法进行更改)。

 /* This is the first option*/
public class Calculation {

    int current = 0;
    int maxNum = 0;
    int minNum;
    int counter=0;
    float sum = 0;
    float avg;

private Scanner scan;

public Calculation(Scanner scan){
     this.scan = scan;
}
public int setCurrent(int current){
   this.current = current;
   return current;
}

public void setMin(int min){
    this.minNum = min;
}

public int getMin(){
    return minNum;
}

public void setMax(int max){
     this.maxNum = max;
}

public void setSum(float sum){
     this.sum += sum;
}

public void minMax(int current){
    setMin(current);
    while(current!=-1){
        setSum(current);;
        if(current>getMin()){
            setMax(current);
        }else if(current<getMin()){
            setMin(current);;
        }

    current = setCurrent(current);;

    counter++;
    }
    System.out.println("The smallest number you entered was: \n" + minNum);
    System.out.println("The biggest number you entered was: \n" + maxNum);
    System.out.println("The sum of all those numbers is: \n" + sum);
    System.out.println("The avarege number is: \n" + (sum/counter));

   }
}

/* Second option */
  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);


    calculation cal1 = new calculation();
    //if the field scan in Calculation is public
    cal1.scan = scan;
    //if it is private
    cal1.setScan(scan);
    System.out.println("Type numbers at will, when finish, type -1 and press    enter");
    cal1.minMax(scan.nextInt());

    scan.close();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM