Java语言程序设计原书第10版(基础版)编程练习题答案 第三章

本篇博客涵盖了Java编程基础版第三章的多个练习题解析,包括解一元二次方程、游戏编程、线性方程求解、随机数生成、日期计算、BMI计算、数字排序、回文数检测、税务计算、猜硬币游戏等,旨在提升初学者的编程能力。
摘要由CSDN通过智能技术生成

*3.1  (代数:解一元二次方程)可以使用下面的公式求一元二次方程ax^{2}+bx+c=0的两个根。

r_{1}=\frac{-b+\sqrt{b^{2}-4ac}}{2a}   和  r_{1}=\frac{-b-\sqrt{b^{2}-4ac}}{2a}

import java.util.Scanner;

public class Exercise3_1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a, b, c: ");
        double a = scanner.nextDouble();
        double b = scanner.nextDouble();
        double c = scanner.nextDouble();
        double delta = b * b - 4 * a * c;
        if (delta > 0) {
            double r1 = (-b + Math.pow(delta, 0.5)) / (2 * a);
            double r2 = (-b - Math.pow(delta, 0.5)) / (2 * a);
            System.out.println("The equation has two roots " + r1 + " and " + r2);

        } else if (delta == 0) {
            double r = (-b) / (2 * a);
            System.out.println("The equation has one root " + r);
        } else {
            System.out.println("The equation has no real roots");
        }
    }
}

 3.2  (游戏:三个数的加法)程序清单3-1中的程序产生两个整数,并提示用户输入这两个整数的和。修改该程序使之能产生三个一位整数,然后提示用户输入这两个整数的和。

public class Exercise3_2 {
    public static void main(String[] args) {
        int number1 = (int)(Math.random() * 10);
        int number2 = (int)(Math.random() * 10);
        int number3 = (int)(Math.random() * 10);

        Scanner input = new Scanner(System.in);

        System.out.print("What is " + number1 + " + " + number2 + " + " + number3 + "? ");

        int answer = input.nextInt();

        System.out.println(number1 + " + " + number2 + " + " + number3 + " = " + answer + " is " +
                        (number1 + number2 + number3 == answer));
    }
}

*3.3  (代数方面:求解2x2线性方程)可以使用编程练习题1.13中给出的Cramer规则解线性方程组:

    编写程序,提示用户输入a、b、c、d、e和f,然后显示结果。如果ad - bc为0,报告消息“The equation has no solution”(方程式无解)。

public class Exercise3_3 {
    public static void main(String[] args) {
        System.out.print("Enter a, b, c, d, e, f: ");
        Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        double d = input.nextDouble();
        double e = input.nextDouble();
        double f = input.nextDouble();
        if (a * d - b * c != 0) {
            double x = (e * d - b * f) / (a * d - b * c);
            double y = (a * f - e * c) / (a * d - b * c);
            System.out.println("x is " + x + " and y is " + y);
        }
        else {
            System.out.println("The equation has no solution");
        }
    }
}

**3.4  (随机月份)编写一个随机产生1和12之间整数的程序,并且根据数字1,2,···,12显示相应月份:January,February,···,December。

public class Exercise3_4 {
    public static void main(String[] args) {
        int month = (int)(Math.random() * 12 + 1);
        switch (month) {
            case 1:
                System.out.println("January"); break;
            case 2:
                System.out.println("February"); break;
            case 3:
                System.out.println("March"); break;
            case 4:
                System.out.println("April"); break;
            case 5:
                System.out.println("May"); break;
            case 6:
                System.out.println("June"); break;
            case 7:
                System.out.println("July"); break;
            case 8:
                System.out.println("August"); break;
            case 9:
                System.out.println("September"); break;
            case 10:
                System.out.println("October"); break;
            case 11:
                System.out.println("November"); break;
            case 12:
                System.out.println("December");
        }

    }
}

*3.5  (找到将来的日期)编写一个程序,提示用户输入代表今天日期的数字(周日为0,周一为1,……,周六为6)。同时,提示用户输入一个今天之后的天数,作为代表将来某天的数字,然后显示这天是星期几。

import java.util.Scanner;

public class Exercise3_5 {
    public static void main(String[] args) {
        System.out.print("Enter today's day: ");
        Scanner input = new Scanner(System.in);
        int today = input.nextInt();
        System.out.print("Enter the number of days elapsed since today: ");
        int theNumberOfDays = input.nextInt();
        int oneDayInTheFuture = (today + theNumberOfDays) % 7;
        String todayStr = "";
        String futureStr = "";
        switch (today) {
            case 0:
                todayStr = "Sunday"; break;
            case 1:
                todayStr = "Monday"; break;
            case 2:
                todayStr = "Tuesday"; break;
            case 3:
                todayStr = "Wednesday"; break;
            case 4:
                todayStr = "Thursday"; break;
            case 5:
                todayStr = "Friday"; break;
            case 6:
                todayStr = "Saturday";
        }
        switch (oneDayInTheFuture) {
            case 0:
                futureStr = "Sunday"; break;
            case 1:
                futureStr = "Monday"; break;
            case 2:
                futureStr = "Tuesday"; break;
            case 3:
                futureStr = "Wednesday"; break;
            case 4:
                futureStr = "Thursday"; break;
            case 5:
                futureStr = "Friday"; break;
            case 6:
                futureStr = "Saturday";
        }
        System.out.println("Today is " + todayStr + " and the future day is " + futureStr);

    }
}

*3.6  (医疗应用程序:BMI)修改程序清单3-4,让用户输入重量、英尺和英寸。例如:一个人身高是5英尺10英寸,输入的英尺值就是5、英寸值为10。

import java.util.Scanner;

public class Exercise3_6 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter weight in pounds: ");
        double weight = input.nextDouble();

        System.out.print("Enter feet: ");
        double feet = input.nextDouble();

        System.out.print("Enter inches: ");
        double inch = input.nextDouble();

        double height = feet * 12 + inch;

        final double KILOGRAMS_PER_POUND = 0.45359237;
        final double METERS_PER_INCH = 0.0254;

        double weightInKilograms = weight * KILOGRAMS_PER_POUND;
        double heightInMeters = height * METERS_PER_INCH;
        double bmi = weightInKilograms /
                (heightInMeters * heightInMeters);

        System.out.println("BMI is " + bmi);
        if (bmi < 18.5)
            System.out.println("Underweight");
        else if (bmi < 25)
            System.out.println("Normal");
        else if (bmi < 30)
            System.out.println("Overweight");
        else
            System.out.println("Obese");
    }
}

3.7  (财务应用程序:整钱兑零)修改程序清单2-10,使之只显示非零的币值单位,用单词的单数形式显示一个单位,例如1 dollar and 1 penny(1美元和1美分;用单词的复数形式显示多于一个单位的值,例如2 dollars and 3 pennies(2美元和3美分)(使用输入值23.67来测试改程序)

import java.util.Scanner;

public class Exercise3_7 {
    public static void main(String[] args) {
        // Create a Scanner
        Scanner input = new Scanner(System.in);

        // Receive the amount
        System.out.print("Enter an amount in double, for example 11.56: ");
        double amount = input.nextDouble();

        int remainingAmount = (int)(amount * 100);

        // Find the number of one dollars
        int numberOfOneDollars = remainingAmount / 100;
        remainingAmount = remainingAmount % 100;

        // Find the number of quarters in the remaining amount
        int numberOfQuarters = remainingAmount / 25;
        remainingAmount = remainingAmount % 25;

        // Find the number of dimes in the remaining amount
        int numberOfDimes = remainingAmount / 10;
        remainingAmount = remainingAmount % 10;

        // Find the number of nickels in the remaining amount
        int numberOfNickels = remainingAmount / 5;
        remainingAmount = remainingAmount % 5;

        // Find the number of pennies in the remaining amount
        int numberOfPennies = remainingAmount;

        // Display results
        System.out.println("Your amount " + amount + " consists of");

        if (numberOfOneDollars == 0) {
            System.out.print("");
        }
        else if (numberOfOneDollars == 1) {
            System.out.println("    " + numberOfOneDollars + " dollar");
        }
        else {
            System.out.println("    " + numberOfOneDollars + " dollars");
        }

        if (numberOfQuarters == 0) {
            System.out.print("");
        }
        else if (numberOfQuarters == 1) {
            System.out.println("    " + numberOfQuarters + " quarter");
        }
        else {
            System.out.println("    " + numberOfQuarters + " quarters");
        }

        if (numberOfDimes == 0) {
            System.out.prin
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值