文章目录
 
 
一、如何使用编程判断闰年
 
- 首先在我们判断闰年之前,我们首先要知道什么是闰年?
- 是闰年有两种情况:能被4整除但是不能被100整除;能被400整除
- 那我们要如何解决闰年判断的问题呢,那我们就涉及到以下三种方法
- 算术运算、关系运算、逻辑运算
- 第一种闰年的情况:year % 4 == 0 && year % 100 != 0
- 第二种闰年的情况:year % 400 == 0
(一、)编程实现
 
- 1、我们首先在net.jia.p02.t02包里创建Task02
  
- 编程如下:
package net.jia.p02.t02;
import java.util.Scanner;
public class Task02 {
    public static void main(String[] args) {
        
        int year;
        String result;
        Scanner sc = new Scanner(System.in);
        
        System.out.print("year = ");
        year = sc.nextInt();
        
        if (year % 4 == 0 && year % 100 == 0 || year % 400 == 0) {
            result = year + "是闰年";
        } else {
            result = year + "是平年";
        }
        
        System.out.println(result);
    }
}
 
- 我们运行一下,查看结果
  
  
- 闰年条件涉及到两种逻辑运算符,逻辑与&&和逻辑或||,但是没有用逻辑非!,我们修改闰年的条件,使三种逻辑运算符都派上用场。
  
(二、)如何使用运算符和表达式
 
 
(三、)随堂训练
 
任务1、计算圆面积(选择结构版)
 
- 利用选择结构防范用户输入负数的圆半径
  
任务2、成绩等级评定
 
- 首先我们需要分清楚分数段
- 第一个分数段:score 
     
      
       
        
         >
        
       
       
        \gt
       
      
     > 100,score 
     
      
       
        
         ∈
        
       
       
        \in
       
      
     ∈ (100,+∞) - 超出范围
- 第二个分数段:90 
     
      
       
        
         ≤
        
       
       
        \le
       
      
     ≤ score 
     
      
       
        
         ≤
        
       
       
        \le
       
      
     ≤ 100,score 
     
      
       
        
         ∈
        
       
       
        \in
       
      
     ∈ [90,100] - 优秀
- 第三个分数段:80 
     
      
       
        
         ≤
        
       
       
        \le
       
      
     ≤ score 
     
      
       
        
         <
        
       
       
        \lt
       
      
     < 90,score 
     
      
       
        
         ∈
        
       
       
        \in
       
      
     ∈ [80,900) - 良好
- 第四个分数段:70 
     
      
       
        
         ≤
        
       
       
        \le
       
      
     ≤ score 
     
      
       
        
         <
        
       
       
        \lt
       
      
     < 80,score 
     
      
       
        
         ∈
        
       
       
        \in
       
      
     ∈ [70,80) - 中等
- 第五个分数段:60 
     
      
       
        
         ≤
        
       
       
        \le
       
      
     ≤ score 
     
      
       
        
         l
        
        
         t
        
       
       
        lt
       
      
     lt 70,score 
     
      
       
        
         i
        
        
         n
        
       
       
        in
       
      
     in [60,70) - 及格
- 第六个分数段:0 
     
      
       
        
         ≤
        
       
       
        \le
       
      
     ≤ score 
     
      
       
        
         <
        
       
       
        \lt
       
      
     < 60,score 
     
      
       
        
         ∈
        
       
       
        \in
       
      
     ∈ [0,60) - 不及格
- 第七个分数段:score 
     
      
       
        
         <
        
       
       
        \lt
       
      
     < 0,score 
     
      
       
        
         ∈
        
       
       
        \in
       
      
     ∈ (-∞,0) - 超出范围
- 在net.jia.p02.t03创建Task03_1
  
- 我们使用level局部变量
  
- 接下来我们应该如何解决这个问题呢?很简单,就是在处理部分之前将level初始化为空字符串
package net.jia.p02.t03;
import java.util.Scanner;
public class Task03_1 {
    public static void main(String[] args) {
        
        int score;
        String level;
        Scanner sc = new Scanner(System.in);
        
        System.out.print("score = ");
        score = sc.nextInt();
        
        level = "";
        if (score > 100) level = "超出范围";
        if (score >= 90 && score <= 100) level = "优秀";
        if (score >= 80 && score < 90) level = "良好";
        if (score >= 70 && score < 80) level = "中等";
        if (score >= 60 && score < 70) level = "及格";
        if (score >= 0 && score < 60) level = "不及格";
        if (score < 0) level= "超出范围";
        
        System.out.println(level);
    }
}
 
- 然后我们运行一下代码块,查看运行的七种情况结果
  
  
  
  
  
  
- 这次我们对我们的代码块进行优化,由于成绩大于100和成绩小于0都是超出范围,所以我们可以将他们并为一列
  
方法二、采用嵌套式多分支结构评定成绩等级
 
- 在net.jia.p02.t03包里创建Task03_2类
  
package net.jia.p02.t03;
import java.util.Scanner;
public class Task03_2 {
    public static void main(String[] args) {
        
        int score;
        String level;
        Scanner sc = new Scanner(System.in);
        
        System.out.print("score = ");
        score = sc.nextInt();
        
        level = "";
        if (score > 100) {
            level = "超出范围";
        } else {
            if ( score >= 90) {
                level = "优秀";
            } else {
                if(score >= 80) {
                    level = "良好";
                } else {
                    if(score >=70) {
                        level = "中等";
                    } else{
                        if(score >= 60) {
                            level = "及格";
                        } else {
                            if(score >= 0){
                                level = "不及格";
                            } else {
                                level = "超出范围";
                            }
                        }
                    }
                }
            }
        }
        
        System.out.println(level);
    }
}
 
 
方法三、采用延拓式多分支结构评定成绩等级
 
- 首先我们在net.jia.p02.t03包里创建Task03_3类
  
package net.jia.p02.t03;
import java.util.Scanner;
public class Task03_3 {
    public static void main(String[] args) {
        
        int score;
        String level;
        Scanner sc = new Scanner(System.in);
        
        System.out.print("score = ");
        score = sc.nextInt();
        
        level = "";
        if (score > 100) {
            level = "超出范围";
        } else if (score >= 90) {
            level = "优秀";
        } else if (score >= 80) {
            level = "良好";
        } else if (score >= 70) {
            level = "中等";
        } else if (score >= 60) {
            level = "合格";
        } else if (score >= 0) {
            level = "不及格";
        } else {
            level = "超出范围";
        }
        
        System.out.println(level);
    }
}
 
- 运行查看结果
  
  
  
  
  
  
  
方法四、采用开关式多分支结构评定成绩等级
 
- 首先我们在net.jia.p02.t03包里创建Task03_4类
  
package net.jia.p02.t03;
import java.util.Scanner;
public class Task03_4 {
    public static void main(String[] args) {
        
        int score;
        String level;
        Scanner sc = new Scanner(System.in);
        
        System.out.print("score = ");
        score = sc.nextInt();
        
        level = "";
        if (score < 0 || score >100) {
            level = "超出范围";
        } else {
            switch (score / 10) {
                case 10:
                case 9:
                    level = "优秀";
                    break;
                case 8:
                    level = "良好";
                    break;
                case 7:
                    level = "中等";
                    break;
                case 6:
                    level = "及格";
                    break;
                default:
                    level = "不及格";
            }
        }
        
        System.out.println(level);
    }
}
 
- 查看运行结果
  
  
  
  
  
  
  
- 本次就聊到这里咯,我们下次再见,记得思考是否还有其他解决的思路!