《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);
    }
}
时间: 2024-09-14 23:14:54

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

《java 语言程序设计》第1章编程练习

1.1 public class test { public static void main(String[] args) { System.out.println("Welcome to java"); System.out.println("Welcome to Computer Science"); System.out.println("Programming is fun"); } } 1.2 public class test {

《java 语言程序设计》第2章编程练习

2.1 public class test { public static void main(String[] args) { Scanner input = new Scanner(System.in); double f, c; c = input.nextDouble(); f = (9.0/5)*c+32; System.out.println(f); } } 2.2 public class test { public static void main(String[] args)

100分求java语言程序设计进阶篇pdf

问题描述 求java语言程序设计进阶篇pdf 解决方案 解决方案二:同求啊!!!解决方案三:这个网上是没有的,我也在网上找过,我建议你去网上找java核心技术<上下卷>pdf这本书写的也是不错的,,这个网上有电子书的,,这两本书配合着java编程思想,相当的不错的解决方案四:真正的进阶是需要项目练习的,纸上得来终觉浅解决方案五:引用2楼xinzailiulei的回复: 这个网上是没有的,我也在网上找过,我建议你去网上找java核心技术<上下卷>pdf这本书写的也是不错的,,这个网上

Java语言的接口与类型安全_JSP编程

接口是实现构件可插入性的关键,可插入构件的关键在于存在一个公用的接口,以及每个构件实现了这个接口. 什么是接口? Java中的接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能). 接口的两种含义:一,Java接口,Java语言中存在的结构,有特定的语法和结构:二,一个类所具有的方法的特征集合,是一种逻辑上的抽象.前者叫做"Java接口",后者叫做"接口"

求java语言程序设计进阶篇课后复习题和编程题答案

问题描述 各位好心人,帮忙发到我邮箱,谢谢了 解决方案

《Python语言程序设计》——第3章数学函数、字符串和对象3.1 引言

第3章 数学函数.字符串和对象 学习目标 使用math模块中的函数解决数学问题(第3.2节). 表示和处理字符串和字符(第3.3-3.4节). 使用ASCII和Unicode对字符编码(第3.3.1-3.3.2节). 使用ord函数获取一个字符的数值编码以及使用chr函数将一个数值编码转换成一个字符(第3.3.3节). 调用带参数end的print函数(第3.3.5节). 使用str函数将数字转换成字符串(第3.3.6节). 使用运算符+来连接字符串(第3.3.7节). 从键盘读取字符串(第3.

《Python语言程序设计》——第2章基本程序设计2.1 引言

第2章 基本程序设计 学习目标 编写程序完成简单的计算(第2.2节). 使用input函数从程序的用户处获取输入(第2.3节). 使用标识符来命名元素,例如:变量和函数等(第2.4节). 将数据赋值给变量(第2.5节). 实现同时赋值(第2.6节). 定义命名常量(第2.7节). 使用运算符+.-.././/.%和*(第2.8节). 编写和计算数字表达式(第2.9节). 利用简捷运算符简化编码(第2.10节). 使用int和round函数进行数据类型转换和四舍五入(第2.11节). 使用time

求 《java语言程序设计_进阶篇》课后完整答案

问题描述 如题,可以的话发到54438561@qq.com.或者给个淘宝网站什么的,我去买也行啊.谢谢啊. 解决方案 解决方案二:不知道,从未看过这类东西

java-Java语言程序设计第4章编程练习题1求问

问题描述 Java语言程序设计第4章编程练习题1求问 import java.util.Scanner; public class Practice { public static void main(String[] args) { Scanner input = new Scanner(System.in); int data = 0; int positive = 0; int negative = 0; int sum = 0; int count = 0; System.out.pri