输出某个年龄的人的比例

package ch06;

import java.math.BigDecimal;
import java.util.Scanner;

/**
 * Created by liwenj on 2017/7/18.
 */
public class test5 {
    //除法

    public static double div(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
//精确的乘
    public static double mul(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int big = 0;
        for (int i = 1; i < 4; i++) {
            System.out.print("请输入第" + i + "个顾客的年龄");
            int age = input.nextInt();
            if (age > 30) {
                big++;
            }
        }
        double r=div(big,3,3);//第二个参数是输入的个数,是第三个参数是保留的小数个数;
        System.out.println(r);
        double x=mul(r,100);//第二个参数是乘数100;
        System.out.println("大于30岁的人" + x + "%");
    }
}

相关推荐