• java-控制语句if/else


    Java if语句用于测试条件。它检查布尔条件为:true 或 false。

    语法格式:

    if(condition){
        //if语句块 => code to be executed.
    }
    //实例:
    public class IfExample{
      public static void main(String [] args){
        int age = 27;
        if (age>18){
          System.out.print("Age is greater than 18.");
        }
      }  
    if(condition){
        //code if condition is true   
    }else{
        //code if condition is false
    }

    //实例:
    public class IfElseExample{
      public static void main(String [] args){
        int number =13;
        if (number % 2 == 0){
          System.out.println("这是一个偶数。");
        }else{
          System.out.println("这是一个奇数。");
        }
      }
    }
    if (){
      //code to be executed if condition1 is true.
    }else if (){
      //code to be executed if condition2 is true.
    }else if (){
      //code to be executed if condition3 is true. }else{
      //code to be executed if all the conditions are false.
    }

    //实例:
    public class IfElseExample{
      public static void main(String [] args){
        int marks = 65;
        if (marks<50){
          System.out.println("fail");
        }else if (marks >= 50 && marks < 60){
          System.out.println("D grade");
        }else if (marks >= 60 && marks < 70){
          System.out.println("C grade");
        }else if (marks >= 70 && marks < 80){
          System.out.println("B grade");
        }else if (marks >= 80 && marks < 90){
          System.out.println("A grade");
        }else if (marks >= 90 && marks <100){
          System.out.println("A+ grade");
        }else{
          System.out.println("Invalid!");
        }
      }
    }
  • 相关阅读:
    前端-【学习心得】-node使用杂谈
    前端-【学习心得】-自己定义一个触摸函数
    前端-【学习心得】-模板渲染的简单方法
    [iOS]NSArray求最大值最小值平均值和的快速方法
    ARC与MRC的混用
    [转]让Xcode的控制台支持LLDB类型的打印
    [iOS]将图片保存到本地相册
    [iOS]深度遍历view的subview
    [转]NSAssert的使用
    [封装]iOS获取设备唯一标识
  • 原文地址:https://www.cnblogs.com/1218-mzc/p/12852729.html
Copyright © 2020-2023  润新知