2020-12-25

本文围绕Java中的选择结构展开,包括if、if…else、if…elseif和switch…case。通过小代码示例讲述各结构用法,如if是单选择结构,用于判断可行性,还介绍了双选择、多选择、嵌套结构及switch匹配字符和字符串的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java中选择结构:if if…esle if …elseif switch…case

本篇文章我将会通过一些小的代码来讲述if if… else if…elseif switch 的用法

if

if是单选择结构,我们很多时候需要去判断一个东西是否可行,这样一个过程用if语句来表示。

语法
if(布尔表达式){
//如果布尔表达式为True将执行的语句
}

package Struct;
import java.util.Scanner;
public class IfDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();

        //equals:判断字符串是否相等
        if(s.equals("hello")){//判断字符串是否相等需要用这个函数  不能用 ==
            System.out.println(s);
        }
        System.out.println("end");

        scanner.close();

    }
}

if …else双选择结构

在这里插入图片描述

if…elseif…多选择结构

在这里插入图片描述

package Struct;

import java.sql.SQLOutput;
import java.util.Scanner;

public class IfDemo03 {
    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(90<=score && score<100){
            System.out.println("A");
        }else if(80<=score && score<90){
            System.out.println("B");
        }else if(70<=score && score<80){
            System.out.println("C");
        }else if(60<=score && score<70){
            System.out.println("D");
        }else{
            System.out.println("不及格");
        }

        scanner.close();
    }
}

if嵌套语句

在这里插入图片描述

switch多选择结构

在这里插入图片描述
匹配字符

package Struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        char grade = 'c';
        //case穿透  匹配一个具体的值
        switch (grade){
            case 'a':
                System.out.println("优秀");
                break;//switch选择结构具有  穿透效应   必须加break
            case 'b':
                System.out.println("良好");
                break;
            case 'c':
                System.out.println("及格");
                break;
            case 'd':
                System.out.println("再接再厉");
                break;
            case 'e':
                System.out.println("挂科");
                break;
            default:
                System.out.println("未知等级");
        }
    }
}

匹配字符串

package Struct;

public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "开心";
        switch (name){
            case "傻蛋":
                System.out.println("傻蛋");
                break;
            case "杨过":
                System.out.println("杨过");
                break;
            case "开心":
                System.out.println("开心");
                break;
            default:
                System.out.println("未搜索到");
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值