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

 

posted @ 2015-03-09 13:09  wuyudong  阅读(459)  评论(0编辑  收藏  举报
Top_arrow