JAVA-分支结构-if条件语句与switch语句

一、首先来介绍两种语法

if条件语句

语法:

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

if(条件语句){
    
    
   //语句块;
 } 
------------------------------------------------------------------------
if (条件语句){
    
    
	//语句块;
 }else{
    
    
	//语句块;
 } 
------------------------------------------------------------------------
if (条件语句){
    
    
	//语句块;
 }else if(条件语句){
    
    
 	//语句块;
 } 
------------------------------------------------------------------------
if (条件语句){
    
    
	//语句块;
 }else if(条件语句){
    
    
	//语句块;
 }else{
    
    
	//语句块;
 }

switch语句

语法:

switch(表达式){
    
    
 case 取值 1:
 			 语句块 1;
 			 break;
case 取值 2: 
			语句块 1;
 		    break;
 		   .
 		   .
 case 取值 n: 
 			语句块 n;
 			break; 
default: 语句块 n+1;
			break; 
}

switch 语句有关规则

①表达式的返回值必须是下述几种类型之一:int, byte, char, short,String;

②case 子句中的取值必须是常量,且所有 case 子句中的取值应是不同

的;

default 子句是可选的;
break 语句用来在执行完一个 case 分支后使程序跳出 switch 语句块;如果 case 后面 没有写 break 则直接往下面执行!
Case 后面的执行体可写{ }也可以不写{ }


如何使用if和switch分支结构

在if和switch两条分支结构,更多的时候是去看情况去选着使用哪一个更合适,下面将举一个例题来说明。

题目:计算该年该月天数

首先是使用if分支结构来完成,由于是完整代码,我们只需要关注重要部分计算输入某年某月的月数这一部分代码即可。

package Level2_4;
import java.util.Scanner;
public class Demo4 {
    
    
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
    
    
		int year = inputYear();// 输入年份
		int month = inputMonth();// 输入月份
		int days = calcDays(year, month);// 计算输入某年某月的月数
		System.out.println(days);
	}
-------------------------------------------------------------------------------------------
	 // 计算输入某年某月的月数
	private static int calcDays(int year, int month) {
    
    
		if (leapYear(year)) {
    
    
			System.out.println("该年为闰年");
			if (month == 2) {
    
    
				return 29;
			} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10
					|| month == 12) {
    
    
				return 31;
			} else {
    
    
				return 30;
			}
		} else {
    
    
			System.out.println("该年为平年");
			if (month == 2) {
    
    
				return 28;
			} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10
					|| month == 12) {
    
    
				return 31;
			} else {
    
    
				return 30;
			}
		}
	}
-------------------------------------------------------------------------------------------
	//判断是否是闰年
	private static boolean leapYear(int year) {
    
    
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
    
    
			return true;
		} else {
    
    
			return false;
		}
	}

	 // 输入月份
	private static int inputMonth() {
    
    
		 System.out.println("请输入月份:");
		
		if (sc.hasNextInt()) {
    
    
			 int num = sc.nextInt();
			if (num >= 1 && num <= 12) {
    
    
				return num;
			} else {
    
    
				System.out.println("输入月份有误,重新操作!");//当信息输入错误时,重新调用并输入。
				return inputMonth();
			}
		} else {
    
    
			System.out.println("你输入的是个锤子!");	 
		}
		return 0;
	}
	 // 输入年份
	private static int inputYear() {
    
    
		System.out.println("请输入年份:");
		if (sc.hasNextInt()) {
    
    
			int num = sc.nextInt();
			return num;
		} else {
    
    
			System.out.println("你输入的是个锤子!");
		}
		return 0;
	}
}

以下是使用switch分支结构

public class Demo4 {
    
    
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
    
    
		int year = inputYear();// 输入年份
		int month = inputMonth();// 输入月份
		calcDays(year, month);// 计算输入某年某月的月数
	}
	-------------------------------------------------------------------------------------------
	  //计算输入某年某月的月数
	private static void calcDays(int year, int month) {
    
    
		switch (month) {
    
    
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			System.out.print("这个月有31天");
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			System.out.print("这个月有31天");
			break;
		case 2:
			if (leapYear(year)) {
    
    
				System.out.print("这个月有29天");
				break;
			} else {
    
    
				System.out.print("这个月有28天");
			}
		}
	}
	-------------------------------------------------------------------------------------------
	// 判断是否是闰年
	private static boolean leapYear(int year) {
    
    
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
    
    
			return true;
		} else {
    
    
			return false;
		}
	}
	 // 输入月份
	private static int inputMonth() {
    
    
		System.out.println("请输入月份:");
		if (sc.hasNextInt()) {
    
    
			int num = sc.nextInt();
			if (num >= 1 && num <= 12) {
    
    
				return num;
			} else {
    
    
				System.out.println("输入月份有误,重新操作!");// 当信息输入错误时,重新调用并输入。
				return inputMonth();
			}
		} else {
    
    
			System.out.println("你输入的是个锤子!");
		}
		return 0;
	}
	  //输入年份
	private static int inputYear() {
    
    
		System.out.println("请输入年份:");
		if (sc.hasNextInt()) {
    
    
			int num = sc.nextInt();
			return num;
		} else {
    
    
			System.out.println("你输入的是个锤子!");
		}
		return 0;
	}
}

总结:

       以上两个做法明显能够感觉到,该题使用switch分支结构更适合。对于ifswitch分支结构,本身结构都不难,难就难在如何选择,所有我们更多的应该是去思考如何去选择这两种分支结构,对于初学者来说需要慢慢多思考一下即可。


猜你喜欢

转载自blog.csdn.net/mjh1667002013/article/details/113408034