• 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.本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    软件 = 程序 + 软件工程(构建之法读书笔记一)
    网站系统开发需要掌握的技术
    C++迪杰斯特拉算法求最短路径
    strcpy函数在VS2015无法使用的问题
    C++哈夫曼树编码和译码的实现
    java学习中一些疑惑解答(2)
    凯撒加密、解密算法
    利用DOS批处理实现定时关机操作
    C及C++中typedef的简单使用指南
    java学习中的一些疑惑解答
  • 原文地址:https://www.cnblogs.com/yanzi-anqi/p/15000026.html
Copyright © 2020-2023  润新知