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

    if条件语句分为三种语法格式,每一种格式都有它自身的特点

    1.if语句

    if语句是指如果满足某种条件,就进行某种处理

    公式:

    if (条件语句){

       执行语句;

       ……

    }

    1 public class Pd{
    2     public static void main(String[] args){
    3         int time = 5;
    4         if(time<7){
    5             System.out.println("闹钟响了,该起床了!");
    6         }
    7     }
    8 }

    结果:会输出闹钟响了,该起床了!

    2.if…else语句

    if (判断条件){

       执行语句1

       ……

    }else{

       执行语句2

       ……

    }

     1 public class Pd {
     2 
     3     public static void main(String[] args) {
     4         int a = 4,b = 7; //定义两个int整型变量
     5         int max;
     6         if(a>b){
     7             max = a;
     8         }else{
     9             max = b;
    10             System.out.println(max);
    11         }
    12     }      
    13 }

    结果:会输出7

    3.if…else if…else语句

    if (判断条件1) {

       执行语句1

    } else if (判断条件2) {

       执行语句2

    }

    ...

    else if (判断条件n) {

       执行语句n

    } else {

       执行语句n+1

    }

     1 public class Pd{
     2     public static void main(String[] args){
     3         int score = 103;
     4         if(score>=90 && score<=100){
     5             System.out.println("优秀");
     6         }else if(score>=60 && score<90){
     7             System.out.println("合格");
     8         }else if(score>=0 && score<60){
     9             System.out.println("不及格");
    10         }else{
    11             System.out.println("错误或异常");
    12         }
    13     }
    14 }

    结果:会输出错误或异常

     

     
  • 相关阅读:
    职业规划书
    阿里云mysql安装配置(CentOS 7.3 64)
    在mvc中弹出提示框
    俺的博客开通啦
    sql中计算列小解
    ext的grid 获取页面内容方式
    2条路 代码生成 or 配置 2.1
    ext做列表页面关于查询多行的办法
    .net下开发windows服务的经验
    微软.net下 charting 要注意的事情
  • 原文地址:https://www.cnblogs.com/ty-v/p/8029033.html
Copyright © 2011-2022 走看看