CSDN博客地址

String类的流程控制

1:复合语句

eg:

     public class Compound{

               public static void main(String[]args){

                         int x=20;

                     {

                          int y=20;

                          System.out.println(y);

                          int z=245;

                          boolean b;

                          {

                                 b=y>z;

                                System.out.println(b);

                           }

              String world="hello java";

              System.out.println(world);

}

}

2:if条件语句

if(布尔表达式){

        语句序列;

        }

eg:

          int a=100;

          if(a==100){

              System.out.println("a的值是:");

            }

   /////////////////////////////

public class Getif{

           public static void main(String[]args){

                     int x=45,y=12;

                     if(x>y){

                      System.out.println("变量x大于y");

                         }

                      if(x<y){

                      System.out.println("变量x小于y");

                          }

   }

}

3:if.......else语句

if(表达式){

       若干语句

             }

else{

          若干语句

}

if()内必须是boolean类型。有时为了编程的需要,else或if后面的“{}"中可以没有语句。

eg:

 public class Getifelse{

            public static void main(String[]args){

                      int math=90,english=56;

                      if(math>60){

                       System.out.println("数学及格了");

                             }

                     else

                          System.out.println("数学没有及格");

                     if(english>60){

                            System.out.println("英语及格了");

                               }

                     else

                           System.out.println("英语未及格");

             }

}

if.....else if多分支语句

  if(条件表达式1){

                语句序列1

}

else if(条件表达式2){

               语句序列2

}

else{

             语句序列3

}

eg:

       public class GetTerm{

                   public static void main(String[]args){

                              int x=20;

                              if(x>30){

                                  System.out.println("x的值>30");

                                        }

                              else if(x>10){

                                 System.out.println("x的值大于10,但小于30");

                                              }

                              else if(x>0){

                                 System.out.println("x的值大于0,但小于10");

                                                }

                               else{

                                      System.out.println("x的值小于0");

                                     }

}

}

switch语句

 switch(表达式){

          case 常量值1:

                    语句模块1;

                  break;

         case 常量值2:

                 语句模块2;

                 break;

      ..............................

       default:

               语句模块n;

              break;

}

eg:

    public class Test{

               public static void main(String[]args){

                         String str="明日科技";

                        switch(str){

                         case "明日":

                                   System.out.println(.........);

                                   break;

                        case "明日科技":

                                   System.out.println("......");

                                    break;

                         default:

                               System.out.println("..............");

                              }

                       }

}

 同一个switch语句,case的常量值必须互不相同

eg:

public class GetSwitch{

            public static void main(String[]args){

                       int week=2;

                       switch(week){

                               case 1:

                                          System.out.println("Monday");

                                          break;

                             case 2:

                                        System.out.println("Tuesday");

                                        break;

                            case 3:

                                        System.out.println("Wendnesday");

                                        break;

                            default:

                                      System.out.println("I don't know");

                    }

}

}

循环语句

while语句也成判断语句,它的循环方式为利用一个条件来控制是否要继续反复执行这个语句。

while(条件表达式){

            执行语句

}

eg:

     public class GetSum{

                 public static void main(String[]args){

                           int x=1;

                           int sum=0;

                           while(x<=10){

                                  sum+=x;

                                   x++;

                                   }

    System.out.println("sum="+sum);

          }

}

不可将while表达式的括号后面加个";"。否则进入死循环

while(x==5);

System.out.println("x的值为5");

这时程序会认为要执行一个语句,而进入无限循环,java编辑器又不会报错。

do....while循环语句

do..while循环语句和while循环语句类似,他们之间的区别是while语句为先判断条件是否成立,再执行语句,而do...while循环语句则先执行一次循环后,再判断条件是否成立,也就是说do...while至少要执行一次。

do{

      执行语句

}

while(条件表达式);

eg:

public class Cycle{

             public static void main(String[]args){

                        int a=100;

                        while(a==60){

                              System.out.println("ok1");

                               a--;

                              }

                       int b=100;

                      do{

                          System.out.println("ok2");

                          b--;

                         }

                         while(b==60);

                     }

}

运行结果:ok2

for循环语句

for(表达式1;表达式2;表达式3){

      语句序列           

}

eg:

       public class Circulate{

                  public static void main(String[]args){

                             int sum=0;

                             for(int i=2;i<=100;i+=2){

                                 sum+=i;

                                }

                             System.out.println("2到100之间的所有偶数和为:"+sum);

                     }

foreach语句

for(元素变量x:遍历对象obj){

              引用了x的Java语句

    }

eg:

   public class Repetition{

              public static void main(String[]args){

                         int arr[]={7,10,1};

                        System.out.println("一维数组中的元素为:");

                        for(int x:arr){

                          System.out.println(x);

                           }

}

}

 

posted @ 2017-08-13 20:33  Yi_warmth  阅读(159)  评论(0编辑  收藏  举报
CSDN博客地址