• 流程控制


    package struct;
    
    public class ShunXunDemo01 {
        public static void main(String[] args) {
            System.out.println("hello1");
            System.out.println("hello1");
            System.out.println("hello1");
            System.out.println("hello1");
            System.out.println("hello1");
        }
    }
    

    按顺序打印出来

    if单选择结构:

    package struct;
    
    import java.util.Scanner;
    
    public class IfDemo01 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入内容:");
            String s = scanner.nextLine();
    
            //equals:判断字符串是否相等
            if (s.equals("Hello")){
                System.out.println(s);
            }
            System.out.println("End");
            scanner.close();
        }
    }
    

    判断字符串是否相等,输出的结果不一样


    if双选择结构:

    package struct;
    
    import java.util.Scanner;
    
    public class IfDemo02 {
        public static void main(String[] args) {
            //考试分数大于60就是及格,小于60分就是不及格
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入成绩:");
            int score = scanner.nextInt();
            if (score>60){
                System.out.println("及格");
            }else{
                System.out.println("不及格");
            }
        }
    }
    

    不同的输入结果不同:


    if多选择结构:

    package struct;
    
    import java.util.Scanner;
    
    public class IfDemo03 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            /*
            * if 语句至多有1个else语句,else语句在所有的else if语句之后
            * if 语句可以有若干个else if 语句,他们必须在else语句之前,
            * 一旦其中一个else if 语句检测为true,其他的else if 以及else语句都将跳过执行
            * */
            System.out.println("请输入成绩:");
            int score = scanner.nextInt();
            if (score==100){
                System.out.println("恭喜满分");
            }else if (score<100 && score>=90){
                System.out.println("A级");
            }else if (score<90 && score>=80){
                System.out.println("B级");
            }else if (score<80 && score>=70){
                System.out.println("C级");
            }else if (score<70 && score>=60){
                System.out.println("D级");
            }else if (score<60 && score>=0){
                System.out.println("不及格");
            }else {
                System.out.println("成绩不合理");
            }
            scanner.close();
        }
    }
    

    嵌套if结构:

    package struct;
    
    import java.util.Scanner;
    
    public class IfDemo04 {
        public static void main(String[] args) {
            //比较两个数的大小
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入第一个数");
            int num = scanner.nextInt();
            System.out.println("请输入第二个数");
            int num1 = scanner.nextInt();
            if (num!=num1){
                if (num>num1) {
                    System.out.println("第一个数大于第二个数");
                }else{
                    System.out.println("第一个数小于第二个数");
                }
            }else {
                    System.out.println("两个数相等");
            }
        }
    }
    




    Switch多选择结构:

    package struct;
    
    public class SwitchDemo01 {
        public static void main(String[] args) {
            //case穿透 //switch 匹配一个具体的值
            char grade = 'C';
            switch (grade){
                case 'A':
                    System.out.println("优秀");
                    break;//可选
                case 'B':
                    System.out.println("良好");
                    break;
                case 'C':
                    System.out.println("及格");
                    break;
                case 'D':
                    System.out.println("再接再厉");
                    break;
                case 'E':
                    System.out.println("挂科");
                    break;
                default:
                    System.out.println("未知等级");
            }
        }
    }
    

    根据grade的值输入不同,输出结果就不同.
    Switch也可以进行字符串比较(JDK7的新特性):

    package struct;
    
    public class SwitchDemo02 {
        public static void main(String[] args) {
            String name = "狂神";
            //jdk7的新特行,表达式结果可以是字符串!!!
            //字符串的本质还是数字
    
            //反编译   java--class(字节码文件)--反编译(IDEA)
    
    
            switch (name){
                case "秦疆":
                    System.out.println("秦疆");
                    break;
                case "狂神":
                    System.out.println("狂神");
                    break;
                default:
                    System.out.println("弄啥嘞!");
            }
        }
    }
    

    while循环结构:

    package struct;
    
    public class WhileDemo01 {
        public static void main(String[] args) {
            //输出1-100
            int i = 0 ;
            while (i<100){
                i++;
                System.out.println(i);
            }
        }
    }
    
    package struct;
    
    public class WhileDemo02 {
        public static void main(String[] args) {
            //死循环
            while (true){
                //等待的客户端链接
                //定时检查
            }
        }
    }
    
    package struct;
    
    public class WhileDemo03 {
        public static void main(String[] args) {
            //计算1+2+3+...+100=?
            int i = 0;
            int sum = 0;
            while (i<=100){
                sum = sum+i;
                i++;
            }
            System.out.println(sum);
        }
    }
    

    do...while循环:

    package struct;
    
    public class DoWhileDemo01 {
        public static void main(String[] args) {
            int i =0;
            int sum = 0;
            do{
                sum = sum + i;
                i++;
            }while(i<=100);
            System.out.println(sum);
            System.out.println(i);
        }
    }
    

    do...while和while的区别:

    package struct;
    
    public class DoWhileDemo02 {
        public static void main(String[] args) {
            //do..while 和while的区别
            int a = 0;
            while (a<0){
                System.out.println(a);
                a++;
            }
            System.out.println("========");
            do {
                System.out.println(a);
                a++;
            }while (a<0);
        }
    }
    

    do..while会先执行一次后在输出结果,while是直接就输出结果

    for循环:

    package struct;
    
    public class ForDemo01 {
        public static void main(String[] args) {
            int a = 1;//初始化条件
    
            while(a<=100){//条件判断
                System.out.println(a);//循环体
                a+=2;//迭代
            }
            System.out.println("While循环结束");
               //初始化值,条件判断,迭代
            for (int i =1;i<=100;i++){
                System.out.println(i);
            }
            System.out.println("for循环结束!");
            /*
             * 关于for循环有一下几点说明:
             * 最先执行初始化步骤.可以声明一种类型,但可以初始化一个或多个循环控制变量,也可以是空语句.
             *最后,检测布尔表达式的值,若果为true,循环体被执行.如果为false,循环终止,开始执行循环体后面的语句
             * 执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减).
             * 再次检测布尔表达式,循环之行上面的过程
             */
            //死循环
            for(;;);
        }
    }
    

    break:

    package struct;
    
    public class BreakDemo {
        public static void main(String[] args) {
            int i = 0;
            while (i<100){
                i++;
                System.out.println(i);
                if (i==30){
                    break;
                }
            }
            System.out.println("123");
        }
    
    }
    

    continue:

    package struct;
    
    public class ContinueDemo {
        public static void main(String[] args) {
            int i = 0;
            while (i<100){
                i++;
                if (i%10==0){
                    System.out.println();
                    continue;
                }
                System.out.println(i);
            }
            //break在任何循环语句的主题部分,均可用break控制循环的流程
            //      break用于强行退出循环,不执行循环中剩余的语句.(break语句也在Switch语句中使用).
            //continue语句用在循环语句体重,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定
        }
    }
    
  • 相关阅读:
    Windows Phone 7 Coding4Fun的弹出框来源:http://www.cnblogs.com/majian714/archive/2011/12/02/2272060.html
    jsp连接mysql的增删改操作
    各种数据库的比较
    jsp连接mysqlupdata操作
    jsp连接Mysql关键代码
    Duwamish学习笔记
    值类型和引用类型的区别
    Factory Method模式的学习
    实现事务的几种方法
    提高Dotnet应用程序性能的技巧
  • 原文地址:https://www.cnblogs.com/linjiangplus/p/15501555.html
Copyright © 2020-2023  润新知