码迷,mamicode.com
首页 > 其他好文 > 详细

第04章 程序设计中的流程控制

时间:2016-11-28 23:44:59      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:default   流程   多态   static   ring   转换   结束   语句   for   

/**
第四章 程序设计中的流程控制

@选择语句

形式一:
if(条件表达式) 单条语句;

形式二:
if(条件表达式){
  语句体;
}

形式三:
if(条件表达式)
{
  语句体;
}else{
  语句体;
}

形式四:
if(条件表达式){
  语句体;
}else if{
  语句体;
}

形式五:
if(条件表达式){
  语句体;
}else if{
  语句体;
}else{
  语句体;
}
==================================================================================
switch(整数因子){
case 整数值1: 语句; break;
case 整数值2: 语句; break;
case 整数值3: 语句; break;
default:语句;
}

public class switche{
  public static void main(String[]args){
    int li_a=2, li_b=0;
    switch(li_a){
      case 1: li_b += 1; break;
      case 2: li_b += 1; //break; //删除试试
      case 3: li_b += 1; break;
      default: li_b += 1; break;
    }
    System.out.println(li_b);
  }
  //删除break;会继续向下执行,直到遇到break;结束。
 }
==================================================================================
@循环语句

for(初始化表达式; 判断表达式; 递增/递减表达式){
语句体;
}

for(type 变量名 : 集合变量名){
  System.out.println(变量名);
}

public class forExp{
  public static void main(String[]args){
    int[] li_arr = {1, 3, 2, 4, 5, 6};
    for(int i=0; i<li_arr.length; i++){
      System.out.println(li_arr[i]);
    }
    System.out.println();
    for(int x : li_arr){
      System.out.println(x);
    }
  }
}
==================================================================================
while(条件表达式){
  语句体;
  {continue;}
}

public class whileX{
  public static void main(String[]args){
    int x = 0;
    while(x < 5){
      x++;
      if (x == 3){continue;}
      System.out.println(x);
    }
  }
}
==================================================================================
//勾子循环语句,最少执行一遍循环。
do{
  语句体;
  {break;}
}while(条件表达式);
==================================================================================

public class test{
  public static void main(String[]args){
    int c;
    test test = new test();
    //错误: 不兼容的类型: void无法转换为int。
    c = test.set(2,3);
    System.out.println(c);
  }
  public void set(int a, int b){
    //错误: 不兼容的类型: 意外的返回值。
    return a * b;;
  }
}

public class test{
  public static void main(String[]args){
    int c;
    test test = new test();
    //错误: 不兼容的类型: void无法转换为int。
    c = test.set(2,3);
    System.out.println(c);
    test.set();
  }
  public int set(int a, int b){
    //错误: 不兼容的类型: 意外的返回值。
    return a * b;
  }
  public void set(){ //这里用到了多态
    int[]arr = {1, 2, 3, 4};
    for(int x : arr){
      System.out.println(x);
    }
    return;
  }
}
*/

第04章 程序设计中的流程控制

标签:default   流程   多态   static   ring   转换   结束   语句   for   

原文地址:http://www.cnblogs.com/QQ9888267/p/6111633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!