JAVA 控制语句

现在举例:
       现在走路,前面是一个大坑,左边是一条好道。实际上就需要有选择。
public class Demo01{
         public static void main(String args[]){
                   // 如果为 true ,则表示愿意掉到坑里去
                   boolean flag = true ;
                   if(flag){
                            System.out.println(" 你想进坑。。 ") ;
                   }
         }
};
 
如果以此程序来看,如果现在选择的不是进坑,就应该选择走好道。
public class Demo01{
         public static void main(String args[]){
                   // 如果为 true ,则表示愿意掉到坑里去
                   boolean flag = false ;
                   if(flag){
                            System.out.println(" 你想进坑。。 ") ;
                   }else{
                            System.out.println(" 走好道。。。 ") ;
                   }
         }
};
 
判断两个数的大小,如果 A 大于 B ,则显示 A 大,如果 A 小于 B ,则显示 B 大,如果相等,则显示相等。
public class Demo02{
         public static void main(String args[]){
                   int i = 10 ;
                   int j = 10 ;
                   if(i>j){
                            System.out.println("I ") ;
                   }else if(i<j){
                            System.out.println("J ") ;
                   }else{
                            System.out.println(" 相等 ") ;
                   }
         }
};
public class Demo03{
         public static void main(String args[]){
                   int score = 5 ;
                   if(score==5){
                            System.out.println("A ") ;
                   }else if(score==4){
                            System.out.println("B ") ;
                  }else if(score==3){
                            System.out.println("C ") ;
                   }else if(score==2){
                            System.out.println("D ") ;
                   }else{
                            System.out.println("E ") ;
                   }
         }
};
 
多条件分支语句( switch 语句)
public class Demo04{
         public static void main(String args[]){
                   int score = 6 ;
                   switch(score){
                            case 5:
                            {
                                     System.out.println("A") ;
                                     break ;
                            }
                            case 4:
                            {
                                     System.out.println("B") ;
                                     break ;
                            }
                            case 3:
                            {
                                     System.out.println("C") ;
                                     break ;
                            }
                            case 2:
                            {
                                     System.out.println("D") ;
                                     break ;
                            }
                            case 1:
                            {
                                     System.out.println("E") ;
                                     break ;
                            }
                            default:
                            {
                                     System.out.println(" 无此内容。。。 ") ;
                            }
                   }
         }
};
 
switch 中的数据类型只能是数字或是字符
public class Demo05{
         public static void main(String args[]){
                   char score = '1' ;
                   switch(score){
                            case '5':
                            {
                                     System.out.println("A") ;
                                     break ;
                            }
                            case '4':
                            {
                                     System.out.println("B") ;
                                     break ;
                            }
                            case '3':
                            {
                                     System.out.println("C") ;
                                     break ;
                            }
                            case '2':
                            {
                                     System.out.println("D") ;
                                     break ;
                            }
                            case '1':
                            {
                                     System.out.println("E") ;
                                     break ;
                            }
                            default:
                            {
                                     System.out.println(" 无此内容。。。 ") ;
                            }
                   }
         }
};
 
循环语句:
       例如:现在假设让一个同学跑圈。
在循环语句之中,必然包括以下三个部分:
       循环的条件初始值
       循环的条件结束值
       循环体
 
例如:
       现在要打印 1~100 中间的全部数:
       循环的条件初始值为 1
       结束的值为 100
       循环体就是不断打印
public class Demo07{
         public static void main(String args[]){
                   int i = 1 ;
                   while(i<=100){
                            System.out.println(i) ;
                            // 修改循环条件
                            i++ ;
                   }
         }
};
 
       此种循环是先在循环体外部进行判断,如果循环条件满足,则进行循环,否则,退出循环,也就是说,此种循环是先判断一次之后再向下继续执行的。
 
以上的循环代码也可以修改为以下的形式:
public class Demo08{
         public static void main(String args[]){
                   int i = 1 ;
                   do{
                            System.out.println(i) ;
                            i++ ;
                   }while(i<=100) ;
         }
};
 
for 循环:
       直接可以在方法体中声明循环的初始值和结束条件,及条件的变更语句;。
public class Demo09{
         public static void main(String args[]){
                   for(int i=1;i<=100;i++){
                            System.out.println(i) ;
                   }
         }
};
 
以上三种循环功能都一样,但是有区别点:
       while do…while 的循环:在不知道循环次数的前提下才可以使用
       for 循环:在已经明确知道了循环次数的情况下可以使用。
 
循环有两种特殊控制语句:一般都要与判断语句一起使用
       break :退出整个操作代码
public class Demo10{
         public static void main(String args[]){
                   for(int i=1;i<=10;i++){
                            if(i==7){
                                     // 退出循环
                                     break ;
                            }
                            System.out.println(i) ;
                   }
         }
};
       continue :退出一次操作代码
public class Demo11{
         public static void main(String args[]){
                   for(int i=1;i<=10;i++){
                            if(i==7){
                                     // 退出循环
                                     continue ;
                            }
                            System.out.println(i) ;
                   }
         }
};
 
continue 下有一个注意点:(了解)
public class Demo11{
         public static void main(String args[]){
         a:       for(int j=0;j<5;j++){
                            for(int i=1;i<=10;i++){
                                     if(i==7){
                                               // 退出循环
                                               // 可以直接给定要退出的循环点
                                               continue a ;
                                     }
                                     System.out.print(i+"\t") ;
                            }
                            System.out.println("") ;
                   }
         }
};
 
练习:
1 、累加   1+2+3+ +200
public class Demo12{
         public static void main(String args[]){
                   int sum = 0 ;
                   for(int i=0;i<100;i++){
                            sum += i ;
                   }
                   System.out.println(sum) ;
         }
};
 
2 、阶乘    20! 1 * 2 * 3 * * 20
public class Demo13{
       public static void main(String args[]){
              int sum = 0 ;
              for(int i=0;i<100;i++){
                     sum *= i ;
              }
              System.out.println(sum) ;
       }
};
 
3 、输出 1000 以内能被 3 7 5 同时整除的数
public class Demo14{
       public static void main(String args[]){
              for(int i=0;i<1000;i++){
                     if(i%3==0&i%5==0&i%7==0){
                            System.out.println(i) ;                 
                     }
              }
       }
};
 
4 、用 * 打印正方形 5 x 5
public class Demo15{
         public static void main(String args[]){
                   for(int i=0;i<5;i++){
                            for(int j=0;j<5;j++){
                                     System.out.print("*") ;
                            }
                            System.out.println("") ;
                   }
         }
};
 
5 、输出 9 * 9 乘法表
public class Demo16{
         public static void main(String args[]){
                   for(int i=1;i<10;i++){
                            for(int j=1;j<=i;j++){
                                     System.out.print(i+"*"+j+"="+(i*j)+"\t") ;
                            }
                            System.out.println("") ;
                   }
         }
};
 
6 、打印三角形
public class Demo17{
         public static void main(String args[]){
                   for(int i=0;i<5;i++){
                            for(int j=0;j<5-i;j++){
                                     System.out.print(" ") ;
                            }
                            for(int j=0;j<i;j++){
                                     System.out.print(" *") ;
                            }
                            System.out.println("") ;l
                   }
         }
};
 

你可能感兴趣的:(java,职场,休闲,控制语句)