zoukankan      html  css  js  c++  java
  • java流程控制

    java流程控制语句
    1.if的三种格式

    格式1:
    public class TestIf {
            public static void main(String[] args) {
                //流程控制语句:if
                //格式1:一般作为单条件判断
                int age = 17;
                if (age>=18) {
                    System.out.println("你已经成年了!");
                }
                System.out.println("over!");
    
            }
        }
    
    格式2:
    
    public class TestIf {
        public static void main(String[] args) {
            //流程控制语句:if
            //格式2:不是失败就是成功,只会执行其中一个,不可能两个都执行
            String username = "admin";
            String password = "admin";
            if (username == "admin" && password =="admin"){
                System.out.println("登录成功!");
            }else{
                System.out.println("登录失败!");
            }
        }
    }
    
    
    格式3:
    public class TestIf {
        public static void main(String[] args) {
            //流程控制语句:if
            //格式3:判断条件2个以上,if ……else if ……else,只会执行一个条件,条件都是互斥的
            int score = 90;
            if (score >= 90 && score <= 100) {
                System.out.println("优");
            } else if (score >= 70 && score < 90) {
                System.out.println("良");
            } else if (score >= 60 && score < 70) {
                System.out.println("及格");
            } else if (score >= 0 && score < 60) {
                System.out.println("不及格");
            } else {
                System.out.println("输入的分数不正确");
            }
            System.out.println("end……");
    
        }
    }    
    

    循环

    • for循环
    public class TestFor {
        public static void main(String[] args) {
            //循环
            /*
            格式:
            for(初始化语句1; 条件判断语句2; 循环体执行后的语句4){
                循环体3;
            }
             */
            //求和
            int s = 0;
            for (int i = 1; i <= 100; i++) {
                s = s + i;
            }
            System.out.println(s);
    
          //遍历数组
          int[] arr = {1, 22, 33, 1213, 213};
          for (int i = 0; i < arr.length; i++) {
              System.out.println(arr[i]);
          }
    
        // 增强for,不能直接拿到索引,只能从头拿到尾,不能倒叙遍历
        //增强for 遍历数组
          /*
          for(数据类型 变量名 : 数组或集合名){
          }
           */
    
          System.out.println("#################");
          for (int e: arr){
              System.out.println(e);
          }
    
        //continue使用
        for (int i = 1; i < 10; i++) {
              if (i % 3 == 0) {
                  continue;   //continue结束本次循环,继续下次循环
              }
              System.out.println(i);
         }
          
        }
    }
    

    while循环*

        public class WhileLoop {
          public static void main(String[] args) {
            //while循环
            /**
             * 初始化语句1
             * while(条件判断语句2){
             *     循环体3;
             *     循环体执行之后的语句4;
             * }
             */
            int i = 1;
            while (i <= 5){
                System.out.println(i);
                i++;
            }
        }
      }
    
    1.作者:yanzi_anqi
    3.本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    转载:对不起,我是程序员
    [VB6]支持UTF文本文件访问的模块 支持UTF8无BOM格式编码自动识别
    Do it. Do it right. Do it right now!
    TortoiseGit密钥的配置
    GIT服务器配置
    Linux下的帧缓冲lcd应用编程及Framebuffer驱动程序模型
    Linux设备驱动程序学习(19)-USB 驱动程序(四)
    是什么浪费了我的上网时间?
    【转】处理wording的一些参考
    ElementUI学习之rules详解
  • 原文地址:https://www.cnblogs.com/yanzi-anqi/p/15000026.html
Copyright © 2011-2022 走看看