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


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

    @选择语句

    形式一:
    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;
      }
    }
    */

  • 相关阅读:
    动态第K大 (树状数组套主席树)
    AC自动机
    开发Jquery插件文字自动截取(提供源代码)
    基于CentOS 5.5,集成LAMP、LNMP精简定制版应用服务器
    MySQL中的mysqldump命令使用详解
    开源CRM和ERP撑起后台系统能走多远
    squid,正向代理,反向代理
    linux架设subversion(svn)版本控制
    浅谈php的urlencode()函数
    读《MYSQL开发者SQL权威指南》笔记
  • 原文地址:https://www.cnblogs.com/QQ9888267/p/6111633.html
Copyright © 2020-2023  润新知