JAVA学习笔记——JAVA语言程序设计第十版第二章习题

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Formatter;
import java.util.Scanner;

public class ExerciseTwo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //创建并初始化Scanner实例           
        Scanner input = new Scanner(System.in);

        System.out.println("2.1题目");

        //提示用户输入摄氏温度
        System.out.println("请输入温度:");
        int centigrade1 = input.nextInt();

        //代入公式转化为 华氏温度
        double fahrenheit1 = (9 / 5.0) * centigrade1 + 32;

        System.out.println("Enter a degree in Centigrade : " + 
                centigrade1);
        System.out.println(centigrade1 + " centigrade is " + 
                fahrenheit1 + " Fahrenheit");
        System.out.println("=========================================================");


        System.out.println("2.2题目");

        //提示用户输入半径 
        System.out.println("请输入半径:");
        double radius2 = input.nextDouble();

//      //提示用户输入高
        System.out.println("请输入高:");
        double length2 = input.nextDouble();

        //代入公式计算面积
        double area2 = radius2 * radius2 * Math.PI;

        //代入公式计算体积
        double volume2 = area2 * length2;

        System.out.println("Enter th radius and length of a cylinder : " 
                + radius2 + " " + length2 + "\n" 
                + "The area is " + (float) area2
                + "The volume is " + (float) volume2);
        System.out.println("=========================================================");


        System.out.println("2.3题目");

        //提示用户输入英尺
        System.out.println("请输入英尺数:");
        double  feet3 = input.nextDouble();

        //转换米
        double meter3 = feet3 * 0.305;

        System.out.println("Enter a value for feet : " 
                + feet3 + "\n" + feet3 
                + " feet is " + meter3 + " meters");
        System.out.println("=========================================================");


        System.out.println("2.4题目");

        //提示用户输入磅数
        System.out.println("请输入重量:(磅)");
        double pounds4 = input.nextDouble();

        //转换千克
        double kilogram4 = pounds4 * 0.454;

        System.out.println("Enter a number in pounds : " 
                + pounds4 + "\n"
                + pounds4 + " pounds is " 
                + kilogram4 + " kilograms");
        System.out.println("=========================================================");

        System.out.println("2.5题目");

        System.out.println("请输入一笔费用:");
        int subtotal5 = input.nextInt();

        System.out.println("请输入酬金率:");
        int gratuityRate5 = input.nextInt();

        float gratuity5 = subtotal5 * (float)gratuityRate5 / 100;
        float total5 = subtotal5 + gratuity5;

        System.out.println("Enter the subtotal and a gratuity rate : " 
                + subtotal5 + " " + gratuityRate5 
                + "\n" + "The gratuity is $" + gratuity5
                + " and total is $" + total5);
        System.out.println("=========================================================");

        System.out.println("2.6题目");

        //死板式衔套if式
        System.out.println("请输入0-1000的任意整数:");
        System.out.println("2.6使用死板方法");

        int number6 = input.nextInt();
        int sum6 = 0;
        int bit6;
        int ten6;
        int hundred6;

        System.out.println("Enter a number between 0 and 1000: " + number6);

        if (number6 == 1000) {
            sum6 = 1;
        } else {

            if (number6 >= 100 && number6 <= 999) {

                //如果输入的数是百位数
                //对十求余取得个位
                bit6 = number6 % 10;

                //利用int类型会截断小数点的特性
                //把输入的数变为十位数
                number6 = number6 / 10;
                ten6 = number6 % 10;

                //同理取得百位数
                number6 = number6 / 10;
                hundred6 = number6 % 10;

                sum6 = bit6 + ten6 + hundred6;
                System.out.println("The sum of the digits is " + sum6);
            } else {

                //当输入的数是十位数时的情况
                if (number6 >= 10 && number6 <= 99) {

                    bit6 = number6 % 10;
                    number6 = number6 / 10;

                    ten6 = number6 % 10;
                    sum6 = bit6 + ten6;

                    System.out.println("The sum of the digits is " + sum6);
                } else {

                    //当输入的数是个位数的情况
                    if(number6 >= 1 && number6 <= 9) {

                        System.out.println("Enter a number between 0 and 1000: " + number6);
                    } else {

                        sum6 = 0;
                        System.out.println("The sum of the digits is " + sum6);
                    }
                }
            }
        }
        System.out.println("=========================================================");

        System.out.println("2.6应用while循环方法");
        System.out.println("Enter a number between 0 and 1000: " + number6);
        /*利用int类型不存储小数点的特性获得非十位以上的数
         * 再将输入的数除以10
         * 缩减末尾数
         * 再相加
         * */
        while(number6 / 10 != 0) {
            sum6 = sum6 + number6 % 10;
            number6 = number6 / 10;
        }

        //输入的数为个位数
        sum6 = sum6 + number6 % 10;
        System.out.println("The sum of the digits is " + sum6);
        System.out.println("=========================================================");

        System.out.println("2.7题目");

        System.out.println("输入分钟数:");
        long minutes7 = input.nextLong();

        //输入的小时数
        long hours7 = minutes7 / 60;

        //输入的天数
        long day7 = hours7 / 24;

        //输入的年数
        long years7 = day7 / 365;

        //多出多少天
        long days7 = day7 - years7 * 365;

        System.out.println("Enter the number of minutes: " 
                + minutes7 + "\n" + minutes7 
                + " minutes is approximately " 
                + years7 + " years and " + days7 + " days");
        System.out.println("=========================================================");

        System.out.println("2.9题目");

        System.out.println("请输入初始速度:(米/秒)");
        float v0 = input.nextFloat();

        System.out.println("请输入终止速度:(米/秒)");
        float v = input.nextFloat();

        System.out.println("请输入时间段:(秒)");
        float t = input.nextFloat();

        System.out.println("Enter v0, v1, and t: " + v0 + " " + v + " " + t);
        float a9 = (v - v0) / t;

        //创建BigDecimal实例,并传入加速度的值
        BigDecimal bd = new BigDecimal(a9);

        //设置BigDecimal的取舍模式
        bd = bd.setScale(4, RoundingMode.HALF_DOWN);

        //创建新的变量存储BigDecimal取舍后加速度的值
        String lastA = bd.toString();

        System.out.println("The average acceleration is " + lastA);
        System.out.println("=========================================================");

        System.out.println("2.10题目");

        System.out.println("请输入重量:(千克,kg)");
        double waterKilograms = input.nextDouble();

        System.out.println("请输入初温:(摄氏度,°C)");
        double temperature = input.nextDouble();

        System.out.println("请输入最终温度:(摄氏度,°C)");
        double finalTemperature = input.nextDouble();

        System.out.println("Enter the amount of water in kilograms: " 
                + waterKilograms 
                + "Enter the initial temperature: " + temperature 
                + "Enter the final temperature: " + finalTemperature);

        double energy = waterKilograms * (finalTemperature - temperature) * 4184;

        System.out.println("The energy needed is " + energy);
        System.out.println("=========================================================");

        System.out.println("2.11题目");

        System.out.println("输入年数:");
        int numberOfYears = input.nextInt();

        //计算输入的年数等于多少秒
        long secondsOfYear = numberOfYears * 365 * 24 * 60 * 60;

        /*算法公式
         * 人口变化数 = 时间 / 诞生时间 - 时间 / 死亡时间 + 时间 / 迁入时间
         */
        long population = 312032486 + secondsOfYear / 7 
                - secondsOfYear /  13 + secondsOfYear / 45;

        System.out.println("Enter the number of years:" + numberOfYears 
                + "\n" + "The population in " + numberOfYears 
                + " years is " + population);

        System.out.println("=========================================================");

        System.out.println("2.12题目");

        System.out.println("请输入速度:(米/秒)");
        float v12 = input.nextFloat();

        System.out.println("请输入加速度:(米/平方秒)");
        float a12 = input.nextFloat();

        double runwayLength = Math.pow(v12, 2) / 2* a9;

        DecimalFormat df9 = new DecimalFormat("0.000");
        df9.setRoundingMode(RoundingMode.HALF_UP);

        System.out.println("Enter speed and acceleration:" + v12 + " " 
                + a12 +"\n" 
                + "The minimum runway length for this airplane is " 
                + df9.format(runwayLength));
        System.out.println("=========================================================");

        System.out.println("2.13题目");

        System.out.println("请输入存入金额:");
        int monthlySave = input.nextInt();

        double monthlyRate = 0.05 / 12;
        System.out.println("请输入定期时间:(月)");
        int monthly = input.nextInt();

        double accountValue = 0;

        for(int i = 1; i <= monthly; i++) {
            accountValue = (monthlySave + accountValue) 
                    * (1 + monthlyRate);    
        }

        NumberFormat nFormat = NumberFormat.getNumberInstance();
        nFormat.setMaximumFractionDigits(2);
        nFormat.setMinimumFractionDigits(2);
        nFormat.setRoundingMode(RoundingMode.HALF_UP);

        System.out.println("Enter the monthly saving amount: " + monthlySave 
                + "\n" + "After the " + monthlySave 
                + " months, the account value is $" 
                + nFormat.format(accountValue));

        System.out.println("=========================================================");

        System.out.println("2.14题目");

        System.out.println("请输入体重:(磅)");
        double pounds14 = input.nextDouble();
        double kilogram14 = pounds14 * 0.45359237;

        System.out.println("请输入身高:(英寸)");
        double inches = input.nextDouble();
        double meters = inches * 0.0254;

        double bmi = kilogram14 / Math.pow(meters, 2);

        DecimalFormat dFormat = new DecimalFormat("0.0000");
        dFormat.setRoundingMode(RoundingMode.HALF_UP);

        System.out.println("Enter weight in piunds: " + pounds14 
                + "\n" + "Enter height in inches: " + inches 
                +  "\n" +"BMI is " + dFormat.format(bmi));
        System.out.println("=========================================================");

        System.out.println("2.15题目");

        System.out.println("请输入x1:");
        double xOne = input.nextDouble();

        System.out.println("请输入y1:");
        double yOne = input.nextDouble();

        System.out.println("请输入x2:");
        double xTwo = input.nextDouble();

        System.out.println("请输入y2:");
        double yTwo = input.nextDouble();

        double firstDistance = Math.pow((xTwo - xOne), 2);
        double secondDistance = Math.pow((yTwo - yOne), 2);
        double  distance = Math.sqrt(firstDistance + secondDistance);

        System.out.println("Enter x1 and y1: " + xOne + " " 
                + yOne + "\n" + "Enter x2 and y2: " + xTwo + " " 
                + yTwo + "\n" + "The distance between the two points is " + distance);
        System.out.println("=========================================================");

        System.out.println("2.16题目");

        System.out.println("请输入六边形的边长:");
        double side1 = input.nextDouble();

        double s1 = Math.pow(side1, 2) * Math.sqrt(3) * 3 / 2;

        DecimalFormat df1 = new DecimalFormat("0.0000");
        df1.setRoundingMode(RoundingMode.HALF_UP);

        System.out.println("Enter the side: " + side1 +"\n" 
                + "The area of hexagon is " + df1.format(s1));
        System.out.println("=========================================================");

        System.out.println("2.17题目");

        System.out.println("Please enter the temperature in Fahrenheit between 58°F and 41°F: ");
        double temperature1 = input.nextDouble();

        System.out.println("Please enter the speed(>=)2 in miles per hour: ");
        double speed1 = input.nextDouble();

        //代入公式
        double temperatureWindChill = 
                35.74 + 0.6215 * temperature1 -
                35.75 * Math.pow(speed1, 0.16) + 
                0.4275 * temperature1 * Math.pow(speed1, 0.16);

        DecimalFormat dFormat2 = new DecimalFormat("0.00000");
        dFormat2.setRoundingMode(RoundingMode.HALF_UP);

        System.out.println("The wind chill index is " + dFormat2.format(temperatureWindChill));
        System.out.println("=========================================================");

        System.out.println("2.18题目");

        System.out.printf("%-5s%-5s%-5s%n", "a", "b", "pow(a, b)");

        for(int i = 1; i < 6; i++) {
            int s = (int)Math.pow(i, i + 1);
            System.out.printf("%-5s%-5s%-5s%n", i, i + 1, s);
        }

        System.out.println("=========================================================");

        System.out.println("2.19题目");

        System.out.println("Enter three point for a triangle: ");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        double x3 = input.nextDouble();
        double y3 = input.nextDouble();

        double sideTri1 = Math.sqrt(
                Math.pow((x2 - x1), 2) + 
                Math.pow((y2 - y1), 2));

        double sideTri2 = Math.sqrt(
                Math.pow((x3 - x2), 2) + 
                Math.pow((y3 - y2), 2));

        double sideTri3 = Math.sqrt(
                Math.pow((x3 - x1), 2) + 
                Math.pow((y3 - y1), 2));

        double s = (sideTri1 + sideTri2 + sideTri3) / 2;

        double area21 = Math.sqrt(s * (s - sideTri1) *
                (s - sideTri2) *
                (s - sideTri3));

        DecimalFormat df19 = new DecimalFormat("0.0");
        df19.setRoundingMode(RoundingMode.HALF_UP);

        System.out.println("The area of the triangle is " +
        df19.format(area21));

        System.out.println("============================================================");

        System.out.println("2.20题目");

        System.out.println("Enter balance and interest rate (e.g., 3 for 3%): ");
        double balance = input.nextDouble();
        double rateOfYear = input.nextDouble();

        double interestRate = balance * (rateOfYear / 1200);

        DecimalFormat dFormat3 = new DecimalFormat("0.00000");
        dFormat3.setRoundingMode(RoundingMode.HALF_UP);

        System.out.println("The interest is " + dFormat3.format(interestRate));
        System.out.println("============================================================");

        System.out.println("2.21题目");
        System.out.println("Enter investment amount: ");
        double investment = input.nextDouble();

        System.out.println("Enter annual interest rate in percentage: ");
        double annualRate = input.nextDouble();

        System.out.println("Enter number of years: ");
        double numberOfYear2 = input.nextDouble();

        double accumulatedValue = investment * Math.pow((1 + annualRate / 12), (numberOfYear2 * 12));

        DecimalFormat df21 = new DecimalFormat("0.00");
        df21.setRoundingMode(RoundingMode.HALF_UP);

        System.out.println("Accumulation value is $" + df21.format(accumulatedValue));
        System.out.println("============================================================");

        System.out.println("2.22题目");
        System.out.println("Enter an amount:");
        int amount = input.nextInt();

        int dolar = amount / 100;
        int cent = amount % 100;

        System.out.println("The amount is " + dolar + "美元" + cent + "美分");

        System.out.println("============================================================");

        System.out.println("2.23题目");

        System.out.println("Enter the driving distance: ");
        double distance23 = input.nextDouble();

        System.out.println("Enter miles per gallon: ");
        double milesPerGallon = input.nextDouble();

        System.out.println("Enter price per gallon: ");
        double price = input.nextDouble();

        double cost = distance23 / milesPerGallon * price;

        DecimalFormat df23 = new DecimalFormat("0.00");
        df23.setRoundingMode(RoundingMode.HALF_UP);

        System.out.println("The cost of driving is $" + df23.format(cost));
    }
}

以上代码,有关DecimalFormate等数字格式化类的知识:http://blog.csdn.net/vinfai/article/details/55517660
以上,有关printf的知识:http://blog.csdn.net/vinfai/article/details/56009415

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值