用Java程序编写100~200之间的能被五整除输出…并求他们的平均值,用整数形式打印输出

2025-01-04 01:55:26
推荐回答(2个)
回答1:

public class Test {

private int sum = 0;
private int count = 0;

public void print(){
int n = 100/5;
// 由于能被5整除的只能是5的倍数这里用求5的倍数的方法来求解
System.out.println("能被5整除的是: ");
for(int i = n;i*5<=200;i++){
System.out.print(i*5+" ");
sum += i*5;
count++;
}
System.out.println();
System.out.println("平均值是 "+ sum/count);

}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t = new Test();
t.print();
}
}

回答2:

public class Devide {
private int i;
private int k=0;
private int sum=0;

public void f() {
System.out.print("能被5整除的数是:");
for(i=100;i<=200;i++){
if(i%5 == 0){
System.out.print(i+" ");
sum += i;
k++;}
}
System.out.println();
System.out.println("平均数是:"+sum/k);
}
public static void main(String[] args) {
Devide a = new Devide();
a.f();
}
}