博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
if选择结构
阅读量:1819 次
发布时间:2019-04-25

本文共 1903 字,大约阅读时间需要 6 分钟。

if选择结构

if单项选择

public class Demon02 {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);​        System.out.println("请输入内容:");        String s = scanner.nextLine();​        //判断字符串是否相等        if (s.equals("Hello")){            System.out.println(s);        }        System.out.println("End");        scanner.close();    }}​
public class Deomn03 {    public static void main(String[] args) {        //考试分数大于60分就是及格,小于60分就是不及格        Scanner scanner = new Scanner(System.in);​        System.out.println("请输入成绩");        int score = scanner.nextInt();//score(成绩)​        if(score>60){            System.out.println("及格");​        }        else{            System.out.println("不及格");         }​​        scanner.close();    }}​

 

  1. if(布尔表达式){

//如果布尔表达式为true将执行的语句

}

  1. if(布尔表达式){

    //如果布尔表达式为true将执行的语句

    }else{

    //如果布尔表达式为flase将执行的语句

    }

  2. if(布尔表达式1){

    //如果布尔表达式为true将执行的语句

    }else if(布尔表达式2){

    //如果布尔表达式为true将执行的语句

    }else if(布尔表达式3){

    //如果布尔表达式为true将执行的语句

    }else{

    //如果以上的布尔表达式都不为true执行代码

    }

public class Demon04 {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        System.out.println("请输入成绩");        int score = scanner.nextInt();        if (score == 100) {            System.out.println("恭喜满分");        } else if (score < 100 && score >= 90) {            System.out.println("A");        } else if (score < 90 && score >= 80) {            System.out.println("B");        } else if (score < 80 && score >= 70) {            System.out.println("C");        } else if (score < 70 && score >= 60) {            System.out.println("D");        } else if (score < 60 && score >= 0) {            System.out.println("不及格");        } else {            System.out.println("成绩不合法");        }        scanner.close();    }    }​
/*if语句至多有一个else语句,else语句在所有的else if语句之后。if语句可以有若干的else if语句,它们必须在else语句之前。一旦其中一个else if语句检测为true,其他的else if以及else语句都将跳过执行。 */

转载地址:http://trukf.baihongyu.com/

你可能感兴趣的文章
C语言atol()函数:将字符串转换成long(长整型)
查看>>
C语言atof()函数:将字符串转换为double(双精度浮点数)
查看>>
角谷步数
查看>>
C语言二级模拟系统
查看>>
乘法算式
查看>>
信用卡号校验
查看>>
立方和等式
查看>>
字符串压缩
查看>>
大数相加
查看>>
随机投点——用蒙特卡洛方法求π的近似值
查看>>
实验一 winrunner的安装使用
查看>>
Problem 1001 有多少个整点
查看>>
Problem 1002 Another A+B Problem
查看>>
Problem 1004 成绩与等级
查看>>
Problem 1005 字符串
查看>>
Problem 1007 字典序
查看>>
Problem 1008 年龄范围
查看>>
Problem 1010 字符串II
查看>>
割圆法求π
查看>>
公式法求π
查看>>