博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《java 语言程序设计》第3、4章编程练习
阅读量:4573 次
发布时间:2019-06-08

本文共 6070 字,大约阅读时间需要 20 分钟。

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);    }}

 

转载于:https://www.cnblogs.com/wuyudong/p/4318000.html

你可能感兴趣的文章
Linux软件安装管理之1——rpm命令管理
查看>>
visual studio 2017 使用笔记
查看>>
iTerm2 半透明颜色主题与字体配置
查看>>
《Entity Framework 6 Recipes》中文翻译系列 (14) -----第三章 查询之查询中设置默认值和存储过程返回多结果集...
查看>>
关于 Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))的问题
查看>>
如何实现一个高效的单向链表逆序输出?
查看>>
JavaScript中严格判断NaN
查看>>
json_encode不自动转义斜杠“/”的方法
查看>>
【转贴】SQL2005的系统表
查看>>
CentOS 7安装PHP依赖管理Composer以及指定PHP版本使用Composer
查看>>
循序渐进大型网站架构
查看>>
Thinkphp5.0支付宝支付扩展库类库大全
查看>>
Nodejs+Express 搭建 web应用
查看>>
2013春节出游兴“专机游”
查看>>
Leetcode 67. Add Binary
查看>>
表达式
查看>>
mysql 创建用户名及密码
查看>>
五 搭建kafka集群
查看>>
Linux 内核即插即用规范
查看>>
【规范】javascript 变量命名规则
查看>>