我有這個類(與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();
}
}
*我已經從主方法和類本身調用了掃描器方法*:不,你沒有。計算中的掃描字段從不在任何地方使用。 –