《java 语言程序设计》第3、4章编程练习

简介:

3.1

public class test {
    public static void main(String[] args) {
        System.out.println("Enter a, b, c: ");
        Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        double delta = b * b - 4 * a * c;
        double t = Math.pow(delta, 0.5);
        if(delta > 0) {
            double x1 = (-b + t) / 2;
            double x2 = (-b - t) / 2;
            System.out.println("The roots are " + x1 + " and " + x2);
        } else if (delta == 0) {
            System.out.println("The root is " + -b / (2 * a));
        } else {
            System.out.println("The equation has no real roots");
        }
    }
}

3.2

public class test {
    public static void main(String[] args) {
        System.out.println("Enter an integer: ");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        System.out.print("Is " + n + " an even number? ");
        if(n % 2 == 0)
            System.out.println("true");
        else
            System.out.println("false");
    }
}

3.3

public class test {
    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();
        double fm = a * d - b * c;
        if(fm == 0) {
            System.out.println("The equation has no solution");
        } else {
            System.out.println("a is " + ((e * d - b * f) / fm) + " and y is " + ((a * f - e * c) / fm));
        }
    }
}

3.4

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = (int)(Math.random() * 100);
        int b = (int)(Math.random() * 100);
        System.out.print("Enter the sum of the two integer(0~100): " + a + " and " + b + ": ");
        int c = input.nextInt();
        if(c == a + b)
            System.out.println("True");
        else
            System.out.println("False");
    }
}

3.5

public class test {
    public static int judge(int year, int month) {
        boolean leap;
        leap = (year % 4 ==0 && year % 100 != 0) || (year % 400 == 0);
        if(month == 2) {
            if(leap) return 29;
            else return 28;
        } else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            return 31;
        } else {
            return 30;
        }
    }
    public static void main(String[] args) {
        String[] months = {" ", "January","February","March","April",
                "May","June","July","August","September",
                "October","November","December"};
        System.out.print("Please inpit month and year: ");
        Scanner input = new Scanner(System.in);
        int month = input.nextInt();
        int year = input.nextInt();
        System.out.println(months[month] + " " + year + " has " + judge(year, month) + " days");
    }
}

4.7

public class test {
    public static void main(String[] args) {double n = 10000;
        double s1, s2, t;
        s1 = s2 = 0;
        t = 1;
        final double rate = 0.05;
        for(int i = 1;  i < 11; i++) {
            t *= (1 + rate);
        }
        s1 = n * t;
        System.out.println("s1 = " + s1);
    }
}

4.16

public class test {
    public static void main(String[] args) {
        System.out.print("Enter a number: ");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int i = 2;
        while(true) {
            while(n % i == 0 && n != i) {
                System.out.print(i + ", ");
                n /= i;
            }
            i++;
            if(n == i) {
                System.out.println(i);
                break;
            }
        }
    }
}

4.25

public class test {
    public static double countPi(int n) {
        double pi = 0;
        double t;
        int m=1;
        for(int i = 1; i < n; i++) {
            t=1.0/(2*i-1);
            t*=m;
            pi+=t;
            m*=-1;
         }
         pi *= 4;
         return pi;
    }
    
    public static void main(String[] args) {
        System.out.print("Enter a number: ");
        Scanner input = new Scanner(System.in);
        for(int i = 10000; i <= 100000; i++) {
            System.out.println("pi(" + i + ") = " + countPi(i));;
        }
    }
}

4.27

public class test {
    public static boolean isLeapYear(int n) {
        return ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0);
    }
    
    public static void main(String[] args) {
        int n = 0;
        for(int i = 2001; i < 2100; i++) {
            if(isLeapYear(i)) {
                n++;
                if(n % 11 == 0) {
                    System.out.println("\n");
                } else {
                    System.out.print(i + " ");
                }
                
            }
        }
    }
}

4.33

public class test {
    public static boolean test(int n) {
        int i, sum;
        int m = n / 2;
        sum = 0;
        for(i = 1; i <= m; i++) {
            if(n % i == 0)
                sum += i;
        }
        if(sum == n)
            return true;
        else
            return false;
    }
    public static void main(String[] args) {
        for(int i = 2; i < 10000; i++) {
            if(test(i))
                System.out.print(i + "\n");
        }
    }
}

4.41

public class test {
    public static void main(String[] args) {
        int n, count , max, t;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number: ");
        n = input.nextInt();
        t = max = n; 
        count = 0;
        while(t != 0) {
            if(t > max) {
                count = 1;
                max = t;
            } else {
                count++;
            }
            System.out.println("Enter a number: ");
            t = input.nextInt();
        }
        System.out.println("max= " + max + ", count= " + count);
    }
}
目录
相关文章
|
26天前
|
SQL Java 数据库
2025 年 Java 从零基础小白到编程高手的详细学习路线攻略
2025年Java学习路线涵盖基础语法、面向对象、数据库、JavaWeb、Spring全家桶、分布式、云原生与高并发技术,结合实战项目与源码分析,助力零基础学员系统掌握Java开发技能,从入门到精通,全面提升竞争力,顺利进阶编程高手。
280 1
|
2月前
|
安全 Java 数据库连接
2025 年最新 Java 学习路线图含实操指南助你高效入门 Java 编程掌握核心技能
2025年最新Java学习路线图,涵盖基础环境搭建、核心特性(如密封类、虚拟线程)、模块化开发、响应式编程、主流框架(Spring Boot 3、Spring Security 6)、数据库操作(JPA + Hibernate 6)及微服务实战,助你掌握企业级开发技能。
344 3
|
26天前
|
Java 开发者
Java并发编程:CountDownLatch实战解析
Java并发编程:CountDownLatch实战解析
343 100
|
1月前
|
算法 Java
Java多线程编程:实现线程间数据共享机制
以上就是Java中几种主要处理多线程序列化资源以及协调各自独立运行但需相互配合以完成任务threads 的技术手段与策略。正确应用上述技术将大大增强你程序稳定性与效率同时也降低bug出现率因此深刻理解每项技术背后理论至关重要.
118 16
|
1月前
|
NoSQL Java 关系型数据库
超全 Java 学习路线,帮你系统掌握编程的超详细 Java 学习路线
本文为超全Java学习路线,涵盖基础语法、面向对象编程、数据结构与算法、多线程、JVM原理、主流框架(如Spring Boot)、数据库(MySQL、Redis)及项目实战等内容,助力从零基础到企业级开发高手的进阶之路。
162 2
|
2月前
|
安全 算法 Java
Java泛型编程:类型安全与擦除机制
Java泛型详解:从基础语法到类型擦除机制,深入解析通配符与PECS原则,探讨运行时类型获取技巧及最佳实践,助你掌握泛型精髓,写出更安全、灵活的代码。
|
2月前
|
安全 Java Shell
Java模块化编程(JPMS)简介与实践
本文全面解析Java 9模块化系统(JPMS),帮助开发者解决JAR地狱、类路径冲突等常见问题,提升代码的封装性、性能与可维护性。内容涵盖模块化核心概念、module-info语法、模块声明、实战迁移、多模块项目构建、高级特性及最佳实践,同时提供常见问题和面试高频题解析,助你掌握Java模块化编程精髓,打造更健壮的应用。
|
2月前
|
Java
Java编程:理解while循环的使用
总结而言, 使用 while 迴圈可以有效解决需要多次重复操作直至特定條件被触发才停止執行任务场景下问题; 它简单、灵活、易于实现各种逻辑控制需求但同时也要注意防止因邏各错误导致無限迁璇発生及及時處理可能発生异常以确保程序稳定运作。
232 0
|
2月前
|
安全 Cloud Native Java
Java:历久弥新的企业级编程基石
Java:历久弥新的企业级编程基石
|
2月前
|
移动开发 Cloud Native Java
Java:历久弥新的企业级编程基石
Java:历久弥新的企业级编程基石

热门文章

最新文章

下一篇
oss教程