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

    流程控制: 顺序结构,  分支结构(if-else   switch-case)  循环结构(while      do....while     for( ; ; ) )

    if- else:执行if-else 语句时,一定会选择其中的一个路径执行,而且也仅会选择一条路径。

    class TestIf1{
    
         public  static void main(String [] args){
                 int age = 28;
    
                 if(age>18){
                       System.out.println(芳芳可能喜欢我);
                  }else{
                 System.out.println("芳芳一定对我有意思了"); 
                 }
               
                 if(age>100 || age< 0){
                  System.out.println("芳芳对我没感觉"); 
                 }else if(age>27){
                  System.out.println("SAY SEEYOU AGAGIN"); 
                 }else{
                 System.out.println("I LIKE FANFANG")
                 }
            }
    }

    练习:

    import java.util.Scanner;
    class
    TestScanner{ public static void main( String[] args) { //创建一个Scanner类的对象 Scanner s = new Scanner(System.in);
    System.out.println("请输入小明的成绩");
    //调用此对象的相应的方法,完成键盘输入值的获取
    //next(): 表示从键盘获取一个字符串
    //String str = s.next();
    int score = s.nextInt();

    if(score >100 || score<0 ){
    System.out.println("你输入的数据有误!");
    }else{
    if(score == 100){
    System.out.println("奖励一次旅游");
    }else if(score > 80 && score <= 99 ){
    System.out.println("奖励一次约会");
    }else if(score > 60 && score <=80){
    System.out.println("口头奖励一次");
    }else{
    System.out.println("什么都没有");
    }
    }
    }
    }

     小结:条件判断之间可以嵌套

               如果多个条件之间是互斥关系,多个条件语句上下顺序是自由的

               如果多个条件之间存在 "包含" 关系   ,要求范围小的在范围大的上面。

    排序:键盘输入是三个整数分别为num1   num2   num3

              对它们进行排序(使用if-else  if-else),并且从小到大输出

    说明:如果if-else  if-else 条件的执行语句块{ }只有一条语句的话, 那么这一对{ }可以省略。

    import java.util.Scanner;
    public class TestIf3 {
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            System.out.println("请输第一个整数");
            int num1 = s.nextInt();
            System.out.println("请输入第二个整数");
            int num2 = s.nextInt();
            System.out.println("请输入第三个整数");
            int num3 = s.nextInt();
    
            // 对num1 num2 num3 小到大排序
            if (num1 > num2) {
                if (num3 > num1) {
                    System.out.println(num2 + "," + num1 + "," + num3);
                } else if (num3 < num2) {
                    System.out.println(num3 + "," + num2 + "," + num1);
                } else {
                    System.out.println(num2 + "," + num3 + "," + num1);
                }
    
            } else {
                if (num3 > num1) {
                    System.out.println(num1 + "," + num2 + "," + num3);
                } else if (num3 < num2) {
                    System.out.println(num3 + "," + num1 + "," + num2);
                } else {
                    System.out.println(num1 + "," + num3 + "," + num2);
                }
            }
        }
    }

    switch-case:

    /*
      switch(变量){
         case 值1:
         case 值2:
         case 值3:
         case 值4:
         case 值5:
      }
     * */
    public class TestSwitch1 {
        public static void main(String[] args) {
            int i = 2;
            switch (i) {
            case 0:
                System.out.println("zero");
            case 1:
                System.out.println("one");
            case 2:
                System.out.println("two");
            case 3:
                System.out.println("three");
            default:
                System.out.println("other");
            }
        }
    }

    输出结果为:

            two
            three
            other

    
    

    注意: switch-case会根据变量的值,选择相应的case去判断,一旦满足case条件,就执行case的相应语句。

                如果没有break或已经到结尾的话,会继续执行其下的case语句。

                default:是可选的,而且位置是灵活的。

                 变量可以有哪些类型? char     byte   short   int   枚举    String (jdk1.7) 

                case 条件:其中条件只能是值,不能是取值范围。

    public class TestSwitch1 {
        public static void main(String[] args) {
            int i = 6;
            switch (i) {
            default:
                System.out.println("other");
            case 0:
                System.out.println("zero");
                break;
            case 1:
                System.out.println("one");
                break;
            case 2:
                System.out.println("two");
                break;
            case 3:
                System.out.println("three");
                break;
            }
        }
    }

    输出结果为:

             other
             zero

    
    
    All that work will definitely pay off
  • 相关阅读:
    git push 时报错 fatal: unable to access 'https://lisir2@github.com/lisir2/react-study.git/': The requested URL returned error: 403
    uniapp请求封装(适合小白,简单易懂,直接复制代码就可以用,像我一般懒得写 就直接复制改改就ok了)
    35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
    34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree
    33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
    32. Path Sum && Path Sum II
    31. Flatten Binary Tree to Linked List
    30. Distinct Subsequences
    29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II
    28. Triangle && Pascal's Triangle && Pascal's Triangle II
  • 原文地址:https://www.cnblogs.com/afangfang/p/12441129.html
Copyright © 2011-2022 走看看