• if与else的结构


    一:基本选择结构if

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

     1 public class anli{
     2                     public static void main(String[] args) {
     3                         Scanner input=new Scanner(System.in);
     4                         System.out.println("请输入张浩的Java考试成绩:");
     5                         int score=input.nextInt();
     6                         //如果成绩大于98分,奖励MP4
     7                         if(score>98){
     8                             System.out.println("奖励MP4");
     9                         }
    10                     

    语法:

    if(条件){

      //代码块

    }

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

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

    二:逻辑运算符:

    &&:并且

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

    ||:或者

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

    !:非

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

    接下来展示案例:张浩的Java成绩大于98分,而且音乐成绩大于80分,或者Java成绩等于100分,音乐成绩大于70老师都会奖励他

     1 public class B3C02 {
     2 
     3 
     4     public static void main(String[] args) {
     5         Scanner input = new Scanner(System.in);
     6         System.out.println("请输入Java的成绩:");
     7         int java=input.nextInt();
     8         System.out.println("请输入muisc的成绩:");
     9         int muisc=input.nextInt();
    10         if((java>98 && muisc>80)||(java==100 && muisc>70)){
    11             System.out.println("奖励一个MP4");
    12         }

    三:if  else

     语法:

        if (条件) {
         //代码块1
        }else{
         //代码块2
        }

    四:多重if选择结构

    语法:

     if ( 成绩>=80) {

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

    案例如下

     1 public class B3C03 {
     2 
     3 
     4     public static void main(String[] args) {
     5         Scanner input = new Scanner(System.in);
     6         System.out.println("请输入成绩:");
     7         int chengji=input.nextInt();
     8         if(chengji>=80){
     9             System.out.println("良好");
    10         }else if(chengji>=60)
    11         {
    12             System.out.println("中等");
    13         }else if(chengji<60){
    14             System.out.println("差");
    15             
    16         }
    17 
    18     }
    19 
    20 }

    五:嵌套if选择结构

    语法:

    if(条件1) {
             if(条件2) {
                   //代码块1
             } else {
                   //代码块2
             }
        } else {
             //代码块3
        }

  • 相关阅读:
    dpkg 删除 百度网盘 程序
    ubuntu 安装go
    解决 swap file “*.swp”already exists!问题
    ROS Topic 常用指令
    正交概念
    vim 永久显示行号 & 临时显示行号
    awk、grep、sed
    Keil中使用Astyel进行C语言的格式化
    红黑树学习
    802.11 对于multicast 和 broadcast的处理
  • 原文地址:https://www.cnblogs.com/yjc1605961523/p/9024322.html
Copyright © 2020-2023  润新知