Java语言程序设计(基础篇)原书第十版 课后习题 第三章

// 自己练习的

// 第三章课后题基本完成,除了一些需要改编的没写(太懒了),而且题目难度不是很大。唉,人就是喜欢给自己的懒惰找借口。
// 题目基本是独立完成,除3.27,3.28参考了第八版课后习题外。
// 拖的时间太长了,晚上时间利用低效,不专注
// 2015/10/9 夜

import java.util.Scanner;
public class Code_Practice_3 {

public static void main(String[] args) {
    // 3.1

    Scanner input = new Scanner(System.in);
    System.out.print("Enter a, b, c:");
    double a = input.nextDouble();
    double b = input.nextDouble();
    double c = input.nextDouble();

    double discriminant = (b * b) - (4 * a * c);

    if (discriminant > 0) {
        double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
        double r2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);
        System.out.println("The equation has two roots " + r1 + " and " + r2);
    }

    else if (discriminant == 0){
        double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
        System.out.println("The equation has one root " + r1);
    }

    else
        System.out.println("The equation has no real roots");

    // 3.4

    int number = (int)(Math.random() * 12) + 1;

    switch (number) {
        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

    Scanner input = new Scanner(System.in);

    System.out.print("Enter today's day:");
    int today = input.nextInt();
    System.out.print("Enter the number of elpased since today:");
    int elpased = input.nextInt();

    switch (today) {
        case 0: System.out.print("Today is Sunday"); break;
        case 1: System.out.print("Today is Monday"); break;
        case 2: System.out.print("Today is Tuesday"); break;
        case 3: System.out.print("Today is Wednesday"); break;
        case 4: System.out.print("Today is Thursday"); break;
        case 5: System.out.print("Today is Friday"); break;
        case 6: System.out.print("Today is Saturday");

    }

    int next = (today + elpased) % 7;
    switch (next) {
        case 0: System.out.println(" and the future day is Sunday"); break;
        case 1: System.out.println(" and the future day is Monday"); break;
        case 2: System.out.println(" and the future day is Tuesday"); break;
        case 3: System.out.println(" and the future day is Wednesday"); break;
        case 4: System.out.println(" and the future day is Thursday"); break;
        case 5: System.out.println(" and the future day is Friday"); break;
        case 6: System.out.println(" and the future day is Saturday");

}

    // 3.8

    Scanner input = new Scanner(System.in);

    System.out.print("Enter three integers:");
    int num1 = input.nextInt();
    int num2 = input.nextInt();
    int num3 = input.nextInt();

    if (num1 > num2) {
        int temp = num1;
        num1 = num2;
        num2 = temp;
    }

    if (num2 > num3) {
        int temp = num2;
        num2 = num3;
        num3 = temp;
    }

    if (num1 > num2) {
        int temp = num1;
        num1 = num2;
        num2 = temp;
    }

    System.out.println(num1 + " " + num2 + " " + num3);

    // 3.9

    Scanner input =  new Scanner(System.in);
    System.out.print("Enter the first 9 digits of an ISBN as integer:");

    int integer = input.nextInt();

    int d1 = integer / 100000000;

// System.out.println(d1); test
int d2 = integer / 10000000 % 10;
int d3 = integer / 1000000 % 10;
int d4 = integer / 100000 % 10;
int d5 = integer / 10000 % 10;
int d6 = integer / 1000 % 10;
int d7 = integer / 100 % 10;
int d8 = integer / 10 % 10;
int d9 = integer % 10;

    int check = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 +
        d5 * 5
  • 14
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值