2016-06-19 18 views
-1

我有這個類(與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(); 

} 

} 
+1

*我已經從主方法和類本身調用了掃描器方法*:不,你沒有。計算中的掃描字段從不在任何地方使用。 –

回答

0

據我所知,你不希望有兩個呼叫new Scanner(System.in);

爲了避免這種情況,您可以簡單地在您的班級計算中寫入: Scanner scan;

,並添加一個構造函數:

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

當然,在main方法,你應該寫: new calculation(scan)

我希望我回答你的問題

注:Java中的類名稱應它應該是Calculation

+0

謝謝你的幫助! –

0

你有這個替代品,你可以讓你的Calculator類與交流使用掃描儀作爲參數,然後將其存儲在一個字段中的onstructor,或者在Calculator類和主文件夾中有一個公共字段,當您獲取掃描程序時會影響此字段(但它應該是私有的,您可以更改它通過getters和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(); 

}