• java基础2 判断语句:if ... else 语句和 switch 语句


    一、if ... else 判断语句

    1、if ... else 判断语句的格式

    1.1、格式

      if(判断条件){

        执行不满足条件的语句

      }

    1.2、格式

      if(判断语句){

        满足条件的语句

      }else{

        执行不满足条件的语句

      }

    1.3、格式

      if(判断条件1){

        满足条件语句1

      }else if(判断条件2){

         满足条件语句2

      }else if(判断条件3){

        满足条件语句3

      }..........else{

            执行不满足条件的语句

      }

    2、举例

    2.1、格式一

    题目:提示用户输入一个人数,如果该整数是5的倍数,打印“5的倍数”如果是2的倍数打印“2的倍数

     1 import java.util.Scanner;
     2 public class Demo9 {
     3     public static void main(String[] args) {
     4         Scanner sc=new Scanner(System.in);
     5         int nextInt = sc.nextInt();
     6         if(nextInt%5==0){
     7             System.out.println("是5的倍数");
     8         }
     9         if(nextInt%2==0){
    10             System.out.println("是2的倍数");
    11         }
    12     }
    13 }

    2.2、格式二

    题目:判断一个整数是奇数还是偶数

     1 public static void main(String[] args) {
     2     Scanner sc = new Scanner(System.in);
     3     System.out.println("请输入一个整数:");
     4     int nextInt = sc.nextInt();
     5     if (nextInt % 2 == 0) {
     6         System.out.println("是偶数");
     7     } else {
     8         System.out.println("是奇数");
     9        }    
    10 }

    2.3、格式三

    题目:提示用户输入一个人数,满分100分,如果满足:60分及格,0--59(不合格),60-69(一般),70-79(良好),80-89(优秀),90-99(学霸),100(学神),就显示相应的提示。

     1 import java.util.Scanner;
     2 class Demo4{
     3     public static void main(String[] args){
     4     //创建一个文本扫描器
     5     Scanner sc=new Scanner(System.in);
     6     System.out.println("请输入你的分数");
     7     int a=sc.nextInt();
     8     if(a>=0 && a<60){
     9     System.out.println("不及格");
    10     }
    11     else if(a>=60 && a<70){
    12       System.out.println("一般,继续努力");
    13     }
    14     else if(a>=70 && a<80){
    15       System.out.println("良好,再接再厉");
    16     }
    17     else if(a>=80 && a<90){
    18       System.out.println("优秀,不要骄傲");
    19     }
    20     else if(a>=90 && a<100){
    21       System.out.println("学霸,努力,下次当学神");
    22     }
    23     else if(a==100){
    24       System.out.println("学神你好");
    25     }
    26     else{
    27       System.out.println("作弊,罚抄1000遍");
    28     }
    29   }   //结果是:看自己输入的值在哪个范围里面,结果就是哪个.
    30 }

     if语句特点

    1. 第二种格式与三元运算符的区别:三元运算符运算完要有值出现。好处是:可以写在其他表达式中。
    2. 条件表达式无论写成什么样子,只看最终的结构是否是true 或者 false

    附:经典题目:判断闰年

    解析:什么是闰年?可以被4整除不能被100整除,或者可以被400整除,那么这一年就是闰年(leap year

     1 public static void main(String[] args) {
     2     Scanner sc = new Scanner(System.in);
     3     System.out.println("请输入年份:");
     4     int year = sc.nextInt();
     5     
     6     // 判断年份能否被4整除
     7     boolean isLeapYear = (year % 4 == 0);
     8     // 年份能被4整除,并且不能被100整除并且使用&&(and)
     9     isLeapYear = isLeapYear && (year % 100 != 0);
    10     // 年份或者能够被400整除
    11     isLeapYear = isLeapYear || (year % 400 == 0);
    12     if (isLeapYear) {
    13         System.out.println(year + "是闰年!");
    14     }
    15     // 简写格式;
    16     if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
    17         System.out.println(year + "是闰年!");
    18     }
    19 }

     二、选择判断语句(switch)

    1、switch语句格式

    switch(表达式){

      case 取值1:

        执行语句;

      break

      case 取值2:

        执行语句;

      break

      …...

      default:

        执行语句;

      break

    }

    2、switch语句特点

       1,switch语句选择的类型只有四种:byteshortint char

       2,case之间与default没有顺序。先判断所有的case,没有匹配的case执行default

       3,switch语句停止的条件是遇到了break关键字或者结束switch语句的大括号

       4,如果匹配的case或者default没有对应的break,那么程序会继续向下执行,运行可以执行的语句,直到遇到break或者switch结尾结束。

       5switch case中的值必须要与switch表达式的值具有相同的数据类型。而且case后跟的值必须是常量,不能跟变量。

    3、举例

    题目:根据用于指定的月份,打印该月份所属的季节.

     1 public static void main(String[] args) {
     2     Scanner sc = new Scanner(System.in);
     3     System.out.println("请输入月份:");
     4     int month = sc.nextInt();
     5 
     6     switch (month) {
     7         case 3:
     8         case 4:
     9         case 5:
    10             System.out.println("spring");
    11         break;
    12 
    13         case 6:
    14         case 7:
    15         case 8:
    16             System.out.println("sunmer");
    17         break;
    18 
    19         case 9:
    20         case 10:
    21         case 11:
    22             System.out.println("autumn");
    23         break;
    24 
    25         case 12:
    26         case 1:
    27         case 2:
    28             System.out.println("winter");
    29         default:
    30             System.out.println("ok");
    31         break;
    32     }
    33 }

    三、总结

    if switch 语句很像具体什么场景下,应用哪个语句呢?

     如果判断的具体数值不多,而是符号byte,short int char 四种类型.

     虽然2个语句都可以使用,建议使用switch语句.因为效率稍高.

    其他情况:

     对区间判断,对结果为boolean 类型判断,使用if if的使用范围更广。

       if 除了能判断具体数值还能判断区间。switch 判断区间会很费劲的。要写好多case 对于运算结果是boolean型的 if 能判断 switch 是不能实现的。例如:根据学生考试成绩划分ABCD   A90-100  B80-89 C70-79 D60-69 E0-59

    实际开发怎么选择呢?

        如果要对具体数值进行判断,并且数值不多,那么 就用switch 来完成。switchcase条件都是编译期整数常量,编译器可以做到表格跳转查询,查找速度快

     但是switch 的局限性比较大必须是4种类型,并且值不多。一般都是使用if。 最后在jdk 7中对switch 进行了增强 还可以判断字符串。5.0 增加了对枚举的判断。

     

     

     

     

    原创作者:DSHORE

    作者主页:http://www.cnblogs.com/dshore123/

    原文出自:http://www.cnblogs.com/dshore123/p/8656358.html

    欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

  • 相关阅读:
    input上传限定文件类型
    扫描二维码自动识别手机系统(Android/IOS)
    css/html/Javascript
    移动端容易碰到的点击穿透的坑
    洛谷P3387 【模板】缩点
    洛谷P1137 旅行计划
    洛谷P2324 [SCOI2005]骑士精神
    洛谷P2571 [SCOI2010]传送带
    BZOJ4300: 绝世好题
    [洛谷P1966] 火柴排队
  • 原文地址:https://www.cnblogs.com/dshore123/p/8656358.html
Copyright © 2020-2023  润新知