• 逆袭之旅DAY20.XIA.选择结构


    2018-07-16  18:50:49

    本章目标:

    • 基本if选择结构
    • 逻辑运算符
    • 多重if选择结构
    • 嵌套if选择结构 

    什么是if选择结构:

      if选择结构是根据条件判断之后再做处理

     1 import java.util.Scanner;
     2 public class GetPrize {
     3     public static void main(String[] args) {
     4         Scanner input = new Scanner(System.in); 
     5              System.out.print("输入张浩的Java成绩: "); //提示输入Java成绩
     6              int score =  input.nextInt();     //从控制台获取张浩的Java成绩
     7              if ( score > 98 ) {                //判断是否大于98分
     8                      System.out.println("老师说:不错,奖励一个MP4!");
     9              }
    10     }
    11 }

    复杂条件下的if选择结构

     1 public class GetPrize2 {
     2     public static void main(String[] args) {
     3         int score1 = 100; // 张浩的Java成绩
     4         int score2 = 72; // 张浩的音乐成绩
     5         if ( ( score1 >98&& score2 > 80 )
     6             || ( score1 == 100 && score2 > 70 ) ){
     7             System.out.println("老师说:不错,奖励一个MP4!");
     8         }
     9     }
    10 }

    if-else  选择结构

    多重if 选择结构

     1 int money = 52; // 我的存款,单位:万元
     2 if (money >= 500) {
     3         System.out.println("太好了,我可以买凯迪拉克");
     4 } else if (money >= 100) {
     5         System.out.println("不错,我可以买辆帕萨特");
     6 } else if (money >= 50) {
     7          System.out.println("我可以买辆依兰特");
     8 } else if (money >= 10) {
     9          System.out.println("至少我可以买个奥托");
    10 } else {
    11         System.out.println("看来,我只能买个捷安特了");
    12 }

    嵌套if的选择结构

    switch

      特点:条件为等值判断

    • case后面的常量值必须各不相同
    • 如果需要每个case执行完以后跳出,在每个case后不要忘记写break;
    • default块顺序可以变动,但要注意其执行顺序。通常,default块放在末尾,也可以省略

     

     

     1     Scanner input = new Scanner(System.in);
     2     if (input.hasNextInt()) {
     3         int num = input.nextInt();
     4         switch (num) {
     5         case 1:
     6             //显示系统主菜单;    
     7             break;
     8         case 2:
     9             System.out.println("谢谢您的使用!");  break;
    10         default:
    11             System.out.println("输入错误。");break;
    12         }
    13     } else {
    14         System.out.println("请输入正确的数字!");
    15     }
     1 switch(day){
     2         case 1: 
     3                System.out.println("法国大餐"); 
     4                break;
     5         case 2:
     6         case 4:
     7                System.out.println("满汉全席");  
     8                break;
     9         case 7:
    10                if (weekOfMonth == 1) {
    11                         System.out.println("苹果餐");
    12                } else {
    13                        System.out.println("香蕉餐");
    14                }
    15                break;
    16 }
    年轻人能为世界年轻人能为世界做些什么
  • 相关阅读:
    在UNICODE编码格式下, CString 转换为 char* :
    inet_ntop(), inet_pton(), htonl(), ntohl()
    'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)'
    NMAKE:fatal error U1077.“...cl.exe” return code 0xc0000135
    拷贝(复制)构造函数和赋值函数
    GIS 地图中术语解释
    Linux 下防火墙端口设置
    LInux 下安装jdk
    ln 命令
    zip命令
  • 原文地址:https://www.cnblogs.com/twinkle-star/p/9319486.html
Copyright © 2020-2023  润新知