zoukankan      html  css  js  c++  java
  • 选择结构

     基本选择结构if

          案例:如果Java考试成绩大于98分则奖励MP4

               public class Demo02 {
                        public static void main(String[] args) {
                                   Scanner input=new Scanner(System.in);
                                   System.out.println("请输入张浩的Java考试成绩:");
                                   int score=input.nextInt();
                                   //如果成绩大于98分,奖励一个MP4
                                 if(score>98){
                                            System.out.println("奖励一个MP4");
                                     }
                                           System.out.println("测试!");
                     }
          }

     语法:

     if (条件){

                   //代码块

    }

       注意:

                  1.条件的结果必须是布尔值

                   2.代码块中只有一条语句时,建议不省略{    }

    2.逻辑运算符:

        &&:并且

                  a && b : a和b两个表达式同时成立(同时为true )整个表达式(a && b)才为true

         ||:或者

                 a || b :a和b两个表达式其中有一个成立时整个表达式(a || b)为true

         !=:非

                 != a : 表达式结果取相反值

      案例:张浩的Java成绩大于98分,而且音乐成绩大于80分,老师会奖励他;或者Java成绩等于100分,音乐成绩大于70分,老师也会奖励他
            public class Demo03 {
                   public static void main(String[] args) {
                           Scanner input=new Scanner(System.in);
                           System.out.println("请输入Java成绩:");
                           //Java成绩
                           int java=input.nextInt();
                           System.out.println("请输入音乐的考试成绩:");
                           int music=input.nextInt();
          
          
                           //如果Java成绩大于98,音乐的成绩大于80才会奖励
                           //a && b :a和b两个表达式同时成立(同时为true)整个表达式(a && b)才为true
                           //a || b :a和b两个表达式其中有一个成立时整个表达式(a || b)为true
                           if((java>98 && music>80)||(java==100&&music>70)){
                                      System.out.println("奖励MP4");
                          }
          
                    }
        }

    lse:

      语法:

                        if(条件)  {

                                    //代码块1

                       }else{

                              //代码块2

                       }

                       当条件为真时会执行代码块一,不会走代码快2

                       当条件为假时会执行代码块二,不会走代码块1

     案例:

                    如果张浩Java考试成绩大于98分,老师就奖励他一个MP4,否则老师就罚他进行编码

                       public class Demo04 {
                               public static void main(String[] args) {
                               //创建一个扫描仪
                               Scanner input=new Scanner(System.in);
          
                               System.out.println("请输入Java考试成绩:");
          
                              int java=input.nextInt();
                             //如果考试成绩大于98分条件真时,会执行代码块1
                             //当条件为假时会执行代码块2
                             if(java>98){
                              //代 码块1
                                      System.out.println("奖励一个MP4");
                            }else{
                                    //代码块2
                                     System.out.println("抄写代码");
                           }
          
                  }
        }

    4.多重if选择结构:
       语法:

                   if ( 成绩>=80) {
                             //代码块1 
                    }
                    else if (成绩>=60) { 
                           //代码块2
                    }
                      else {
                       //代码块3 
                       }
                    成绩>=80为条件1
                    成绩>=60为条件2

                    如果条件1结果为真时会执行代码块1,代码块1执行完毕后不会再去判断其他条件,直接执行多重if选择结构以下的代码
                     如果条件1结果为假时会再次判断条件2,如果条件2为真时会执行代码块2,代码块2执行完毕后不会再去判断其他条件,直接执行多重if选择结构以下的代码
                    如果条件2结果为假时会执行代码块3,代码块3执行完毕后,直接执行多重if选择结构以下的代码
       案例:     

         5.嵌套if选择结构:
         语法:
                  if(条件1) {
                if(条件2) {
                   //代码块1
               } else {
                     //代码块2
            }
               } else {
                      //代码块3
               }
                如果条件1为真时,去判断条件2,如果条件2为真时会执行代码块1,如果条件2为假时会执行代码块2
                  如果条件1为假时回去执行代码块3
       案例:学校举行运动会,百米赛跑跑入10秒内的学生有资格进决赛,根据性别分别进入男子组和女子组
        public class Demo07 {
                 public static void main(String[] args) {
                         Scanner input=new Scanner(System.in);
                         System.out.println("请输入成绩:");
                         int score=input.nextInt();
          
                          if(score<=10){
                                  System.out.println("进入决赛!");
                                  System.out.println("请输入您的性别:");
                                String sex=input.next();
                               if(sex.equals("男"))
                                { 
                                   System.out.println("进入男子组决赛");
                               }else{
                                  System.out.println("进入女子组决赛");
                                   }
           
                  }else
             {
                                  System.out.println("你太慢了!,没有进入决赛");
                            }
                 }
         
        }

           案例1.
                  public class Demo02 {
                         public static void main(String[] args) {
                                  Scanner input=new Scanner(System.in);
                                   System.out.println("请输入张浩的Java考试成绩:");
                                   int score=input.nextInt();
                                  //如果成绩大于98分,奖励一个MP4
                                  if(score>98){
                                         System.out.println("奖励一个MP4");
                                     }
                                         System.out.println("测试!");
                       }
              }

        案例2:

        public class Demo03 {
                   public static void main(String[] args) {
                             Scanner input=new Scanner(System.in);
                             System.out.println("请输入Java成绩:");
                            //Java成绩
                           int java=input.nextInt();
                            System.out.println("请输入音乐的考试成绩:");
                             int music=input.nextInt();
          
          
                             //如果Java成绩大于98,音乐的成绩大于80才会奖励
                             //a && b :a和b两个表达式同时成立(同时为true)整个表达式(a && b)才为true
                             //a || b :a和b两个表达式其中有一个成立时整个表达式(a || b)为true
                            if((java>98 && music>80)||(java==100&&music>70)){
                                    System.out.println("奖励MP4");
                               }
          
                      }
        }

       案例3:

             public class Demo04 {
                      public static void main(String[] args) {
                              //创建一个扫描仪
                              Scanner input=new Scanner(System.in);
          
                             System.out.println("请输入Java考试成绩:");
          
                             int java=input.nextInt();
                            //如果考试成绩大于98分条件真时,会执行代码块1
                            //当条件为假时会执行代码块2
                            if(java>98){
                                     //代码块1
                                    System.out.println("奖励一个MP4");
                           }else{
                                    //代码块2
                                   System.out.println("抄写代码");
                            }
          
                     }
        }

    案例 3:

           public class Demo07 {
                    public static void main(String[] args) {
                              Scanner input=new Scanner(System.in);
                             System.out.println("请输入成绩:");
                            int score=input.nextInt();
          
                            if(score<=10){
                                     System.out.println("进入决赛!");
                                     System.out.println("请输入您的性别:");
                                     String sex=input.next();
                                     if(sex.equals("男"))
                                     {
                                               System.out.println("进入男子组决赛");
                                   }else{
                                              System.out.println("进入女子组决赛");
                                   }
           
                         }else
                          {
                                         System.out.println("你太慢了!,没有进入决赛");
                                  }
                        }
         
        }

  • 相关阅读:
    相亲数问题
    MySQL导入导出命令
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/szhhhh/p/9024397.html
Copyright © 2011-2022 走看看