javaSE基础知识&条件判断

1.条件语句 【 if 】

在这里插入图片描述

语法:
if(条件) {
条件成立时执行语句
}

代码:

class Demo{
	public static void main(String []args){
		int a = 10;
		if(a > 5){
		System.out.println("trun");
		}
	}
}

输出结果为:turn

------------------------------------分----------------------------割------------------------------------线----------

2.条件语句【 if…else 】

在这里插入图片描述

语法:
if(boolean类型的条件表达式) {
语句块1
} else {
语句块2
}

代码:

package Demo;

public class Demo4 {
	public static void main(String[] args) {
	
		int a = 10;
		if(a>15) {
			System.out.println("turn");
		}else {
			System.out.println("flase");
		}
	}
}

输出结果为:flase

------------------------------------分----------------------------割------------------------------------线----------

3.条件语句【 多重 if 】

在这里插入图片描述

语法:
if(条件1) {
语句块1}else if (条件2){
语句块2}else if(条件3){
语句块3}

代码:

package Demo;

public class Demo4 {
	public static void main(String[] args) {
	
		int a = 16;
		
		if(a > 15) {
			System.out.println("A");
		}else if(a > 10) {
			System.out.println("B");
		}else if(a > 5) {
			System.out.println("C");
		}
	}
}

输出结果为: A

------------------------------------分----------------------------割------------------------------------线----------

4.条件语句【 嵌套 if 】

执行过程:
在这里插入图片描述

if(条件1) {
if(条件2) {
语句块1 } else {
语句块2 } else {
语句块3 }

package Demo;

public class Demo4 {
	public static void main(String[] args) {
	
		int a = 6;
		
		if(a>5) {
			if(a>7) {
				System.out.println("A");			
			}else {
				System.out.println("B");
				} 		
			}else {
				System.out.println("C");
			}
		}
	}

输出结果为: B

5.等值判断【switch & case】

条件循环语句: switch (选择性执行,不满足以下3个条件时执行default语句)

代码:

package Demo;

public class Demo4 {
	public static void main(String[] args) {
	
	     int age = 3;      //   变量值

	     switch(age) {

	         case 1:
	             System.out.println("A");
	             break;    // 跳出循环
	       
	          case 2:
	             System.out.println("B");
	             break;    // 跳出循环
	       
	         case 3:
	             System.out.println("C");
	              break;    // 跳出循环

	     default :
	         System.out.println ("错误");
	     
		}
	}
}

输出结果为:C

猜你喜欢

转载自blog.csdn.net/qq_44323273/article/details/86291823