JAVA语言程序设计课后习题----第一单元解析(仅供参考)

1  本题是水题,基本的输出语句

1 public class test {
2     public static void main(String[] args) {
3 //      相邻的两个 "" 要用 + 相连
4 //        \t相当于tab键
5         System.out.println("xxx"+"\t"+"18岁");
6     }
7 }

2  本题可以用一个for循环来实现循环十次即可

 1 public class test {
 2     public static void main(String[] args) {
 3         int sum=0;
 4 //        定义一个变量sum来接收每次相加的结果
 5 //        for循环10次,从1开始每次都把值与sum相加
 6         for (int i = 1; i <=10 ; i++) {
 7             sum += i;
 8         }
 9         System.out.println("1+2+...+10="+sum);
10     }
11 }

3  本题考的就是圆的周长、面积公式的运用

 1 public class test {
 2     public static void main(String[] args) {
 3 //        PI的用法
 4        double area,az,r=5.5;
 5        area=Math.PI*r*r;
 6        az=Math.PI*r*2;
 7         System.out.println("园的面积为:"+area);
 8         System.out.println("园的周长为:"+az);
 9     }
10 }

4  本题考的是双重for循环,一个循环控制行数,一个循环控制每行输出几个*

 1 public class test {
 2     public static void main(String[] args) {
 3        /*
 4        * 第一层for循环循环5次代表着有五行
 5        * 第二个for循环代表着每次循环几次*/ 
 6         for (int i = 1; i <=5 ; i++) {
 7             for (int j = 1; j <=i; j++) {
 8                 System.out.print("*");
 9             }
10             System.out.println();
11         }
12     }
13 }

 

posted @ 2019-05-14 22:08  不愿当个小萌新  阅读(222)  评论(0编辑  收藏  举报