JAVA语言程序设计基础篇(Chapter 1)课后习题参考答案

1. (简答题)(Display five messages) Write a program that displays Welcome to Java five times.

public class Demo2 {
    public static void main(String[] args) {
        for(int i = 0;i<5;i++) {
            if (i<5) {
                System.out.println("Welcome to Java");
            }
        }
        
    }

}

2. (简答题)(Summation of a series) Write a program that displays the result of 

              1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9.

public class Demo3 {
    public static void main(String[] args) {
        System.out.println(1+2+3+4+5+6+7+8+9);
    }
   

}

3. (简答题)(Approximate PI) PI can be computed using the following formula:

Write a program that displays the result of 

and

Use 1.0 instead of 1 in your  program.

public class Demo4 {
    public static void main(String[] args) {
        System.out.println(4*(1.0-1.0/3+1.0/5-1.0/7+1.0/9-1.0/11));
        System.out.println(4*(1.0-1.0/3+1.0/5-1.0/7+1.0/9-1.0/11+1.0/13));
    }
    
}

4. (简答题)(Algebra: solve 2 * 2 linear equations) You can use Cramer’s rule to solve the following 2 * 2 system of linear equation:

Write a program that solves the following equation and displays the value for x and y:

public class Demo5 {
    public static void main(String[] args) {
        double x , y , a , b , c , d , e , f;
        a = 3.4;
        b = 50.2;
        c = 2.1;
        d = 0.55;
        e = 44.5;
        f = 5.9;
        
        x = (e*d-b*f)/(a*d-b*c);
        y = (a*f-e*c)/(a*d-b*c);
        System.out.println("x="+x);
        System.out.println("y="+y);
            
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值