这是一个资费问题,建立1个数组里随机生成20个量,并以18元和10元进行分类
import java.util.Random;
public class 资费 {
public static double getMailFee(int weight) {
double fee=0;
if(weight<=100) {
fee=Math.ceil(weight/20.0)*1.2;
}else {
fee=Math.ceil((weight-100)/100.0)*2+(100/20)*1.2;
}
return fee;
}
public static void main(String [] args) {
int []a =new int [20];
Random r=new Random();
for (int i=0;i<a.length;i++) {
a[i]=r.nextInt(2000)+1;
}
for(int i=0 ;i<a.length;i++){
System.out.println("a["+i+"]="+a[i]);
}
int b1=0,b2=0;
for (int i=0;i<a.length;i++) {
double fee=getMailFee(a[i]);
System.out.println("当前函数重量"+a[i]+"资费为"+fee);
if(fee<=10) {
b1++;
}else {
b2++;
}
}
System.out.println("不足10元的函件个数:"+b1);
System.out.println("超过10元的个数:"+b2);
}
}
运行结果如下: