day03

主要涉及内容:

  swith; if; 

  while; do while; for;

  break; contiune;

  略微涉及函数。

关于switch的内容

 1 public class SwitchTest {
 2     
 3     public static void main(String[] args) {
 4         int a = 2;
 5         switch(a){      //a这个位置的值必须是整数家族(除了long,太长了),String,枚举。
 6             case 1:     //当a == 1 的时候,执行下面的语句,注意case后面是有冒号的
 7                 System.out.println("这里是数字1。。。");
 8                 break;    //执行完语句后,要跳出,否则或执行下面的语句。
 9 10             case 2:     //当a == 2 的时候,执行下面的语句
11                 System.out.println("这里是数字2。。。");
12                 break;
13 14             case 3:     //当a == 3 的时候,执行下面的语句
15                 System.out.println("这里是数字3。。。");
16                 break;
17 18         }
19     }
20 }

关于for循环的内容:

 1 public class Number {
 2     //从命令行获取一个数字并判断这个数字是不是质数
 3     public static void main(String[] args) {
 4         int num = Integer.parseInt(args[0]);
 5         boolean flag = true;
 6         for(int i = 2; i < num; i++) {
 7             if(num % i == 0){
 8                 flag = false;
 9                 break;
10             }
11         }
12         if(flag) {
13             System.out.println(num + "是质数。");
14         } else {
15             System.out.println(num + "不是质数。。。");
16         }
17     }
18     
19 }

for循环的嵌套,以及break的使用位置:

 1 public class Number2 {
 2     //从命令行获取一个数字,找出3到该数字之间的所有质数
 3     public static void main(String[] args) {
 4         int num = Integer.parseInt(args[0]);
 5         for(int i = 3; i <= num; i++) {
 6             boolean flag = true;
 7             for(int j = 2; j < i; j++) {
 8                 if(i % j == 0) {
 9                     flag = false;
10                     break;
11                 }
12             }
13             if(flag) {
14                 System.out.println(i + "是质数。。。");
15             }
16         }
17     }
18     
19 }

粗略的设计函数:

 1 public class FuncTest {
 2     
 3     public static int avg(int a, int b) {    //应以一个函数,名字叫做avg;
 4         return (a + b) / 2;                    //方法里面进行的运算;
 5     }
 6     
 7     public static void main(String[] args) {
 8         int avgNum = avg(4, 6);                //调用avg函数,病出入两个参数用avgNum接收;
 9         System.out.println(avgNum);
10     }
11     
12 }

 打印一个底边为a的等腰三角形

 1 public class Triangle {
 2     //打印一个底边为a的等腰三角形
 3     public static void main(String[] args) {
 4         int a = Integer.parseInt(args[0]);
 5         for(int i = 1; i < (a - 1) / 2 + 1; i++) {
 6             for(int j = 1; j <= (a - 1) / 2 - i + 1; j++) {
 7                 System.out.print(" ");
 8             }
 9             System.out.print("*");
10             if(i == 1) {
11                 System.out.println();
12                 continue;
13             }
14             int j = 1;
15             while(j <= (2 * (i - 2) + 1)) {
16                 System.out.print(" ");
17                 j++;
18             }
19             System.out.println("*");
20         }
21         for(int i = 1; i <= a; i++) {
22             System.out.print("*");
23         }
24     }
25     
26 }
原文地址:https://www.cnblogs.com/alwaysInMe/p/6365887.html