Java学习历程三《流程控制语句-选择结构》

流程控制语句

  • 顺序
  • 选择
  • 循环
  1. 顺序,从上到下,一句一句的执行代码
  2. 选择,分支结构
  3. 循环结构

选择结构

  • if结构,if-else结构
  • 多重if
  • 嵌套if
  • switch结构
多重if
import java.util.Scanner;

public class xueshengchengji {
    public static void main(String[] args) {
        /*
        >90 优
        >=80  <90 良
        >=60 <80 中
        <60   差
        */
        System.out.println("请输入您的成绩:");
        Scanner sc = new Scanner(System.in);
        int score = sc.nextInt();
        if (score >= 90) {
            System.out.println("优");
        }
        if (score >= 80 && score < 90) {
            System.out.println("良");
        }
        if (score >= 60 && score < 80) {
            System.out.println("中");
        }
        if (score < 60) {
            System.out.println("差");
        }

    }
}

else-if结构,在上述条件不成立的情况下,如果当前条件
成立,才算成立

import java.util.Scanner;

public class xueshengchengji {
    public static void main(String[] args) {
        /*
        >90 优
        >=80  <90 良
        >=60 <80 中
        <60   差
        */
        System.out.println("请输入您的成绩:");
        Scanner sc = new Scanner(System.in);
        int score = sc.nextInt();
        if (score >= 90) {
            System.out.println("优");
        }
        else if (score >= 80) {
            System.out.println("良");
        }
        else if (score >= 60) {
            System.out.println("中");
        }
        else{
            System.out.println("差");
        }

    }
}

嵌套if结构
public class IntCompare {
    public static void main(String[] args) {
        //
        int x = 5, y = 10;
        if (x != y) {
            if (x > y) {
                System.out.println(x + ">" + y);
            } else {
                System.out.println(x + "<" + y);
            }
        } else {
            System.out.println(x + "和" + y + "相等");
        }

    }
}
switch结构

和if结构的区别主要在于判断条件的类型 if结构条件是boolean 而switch是常量值

结构表达式

switch(表达式){
    case 常量表达式1:
    语句1;break;
    case 常量表达式2:
        语句2;break;
    default:
        语句3;
}

break会保证执行完成当前分支后,跳出switch语结构,如果没有
break,会将剩余分支按顺序执行完毕

import java.util.Scanner;
public class weekday {
    public static void main(String[] args) {
        //
        System.out.println("请输入1-7数字:");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        switch (n) {
            case 1:
                System.out.println("周一");
                break;
            case 2:
                System.out.println("周二");
                break;
            case 3:
                System.out.println("周三");
                break;
            case 4:
                System.out.println("周四");
                break;
            case 5:
                System.out.println("周五");
                break;
            case 6:
                System.out.println("周六");
                break;
            case 7:
                System.out.println("周日");
                break;
            default:
                System.out.println("该数字超出范围");
        }
    }
}

//获取字符串输入内容,并转换成为大写
Scanner sc = new Scanner(System.in);
String st = sc.next();
st = st.toUpperCase()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值