仅使用主方法的扫描仪

时间:2016-06-19 12:55:39

标签: java class while-loop java.util.scanner main

我有这个类(有setter,getters和一个方法),无限期地向用户询问一个数字,直到他输入-1。 我从主要方法和类本身调用了扫描程序方法,有一种方法只能从主方法调用扫描程序方法一次,并将输入应用于类每次需要它?我非常感谢你的帮助。如果不清楚,请与我联系。

这是“类别代码”:

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

}

}

这是主要的方法代码:

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

}

}

2 个答案:

答案 0 :(得分:0)

据我了解,您不希望两次致电new Scanner(System.in);

为避免这种情况,您可以在课堂计算中简单地写: Scanner scan;

添加构造函数:

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

当然,在主要方法中你应该写: new calculation(scan)

我希望我回答你的问题

注意:在Java中,您的类名称应以大写字母开头,应为Calculation

答案 1 :(得分:0)

你有一些替代方法,你可以让你的Calculator类带有一个构造函数,它将Scanner作为一个参数,然后将它存储在一个字段中,或者你可以在Calculator类和main中有一个公共字段。你得到扫描仪只会影响这个字段(但它应该是私有的,你可以通过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();

}