顺序结构

分支结构

循环结构

Java分支结构:if-else、switch-case

if-else:

/*
分支结构包括:if-else 和switch - case
分支结构中的if-else(条件判断结构)
1.三种形式

第一种:
if(条件表达式){
	执行表达式
}

第二种:二选一
if(条件表达式){
	执行表达式1
}else{
	执行表达式2
}

第三种:多选一
if(条件表达式){
	执行表达式1
}else if(条件表达式){
	执行表达式2
}else if(条件表达式){
	执行表达式3
}else{
	执行表达式4
}
*/
class IfTest {
	public static void main(String[] args) {	
		//举例1
		int heartBears =179;
		if(heartBears < 60 || heartBears>100){
			System.out.println("你需要做进一步的检查");
		}
		System.out.println("检查结束");
	}
}

//***************************************
//练习1
class Test{
	public static void main(String[] args){
		int a = 45636;
		int b = 637;
		int c = 6454;
		//选出较大的数
		//分支结构 if - else
		int max;
		if(a > b && a > c){
			max = a;
		}else if(b > a && b > c){
			max = b;	
		}else{
			max = c;
		}
		System.out.println(max);
		//int max1 = (i1 > i2)? i1 : i2;
		//int max = (max1 > i3)? max1 : i3;
		//System.out.println(max);
	}
}
//************************************
//练习2
class Test1{
	public static void main(String[] args){
		double d1 = 7.9;
		double d2 = 724.3;
		if (d1 > 10.0 && d2 < 20.0){
			System.out.println(d1 + d2);
		}else{
			System.out.println(d1 * d2);
		}
	}
}

如何从控制台获取不同类型的变量:需要使用Scanner类:

/*
如何从控制台获取不同类型的变量:需要使用Scanner类

具体实现步骤:
1.导包:import java.util.Scanner;
2.Scanner的实例化
3.调用Scanner类的相关方法(next() 或 nextxXX())获取指定类型的变量

*/
import java.util.Scanner;//导包  导入了Scanner这个类
class ScannerTest {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in); // 创建(new)了一个对象
		
		System.out.println("请输入你的姓名:");
		String num = scan.next();// 对象调用了方法
		System.out.println(num);

		System.out.println("请输入你的年龄:");
		int year = scan.nextInt();
		System.out.println(year);

		System.out.println("请输入你的体重:");
		double weight = scan.nextDouble();
		System.out.println(weight);

		System.out.println("你爱我吗?(true/false)");
		boolean isLove = scan.nextBoolean();
		System.out.println(isLove);
	}
}

IfTest:

/*
说明:最后的else是可选的。
说明:一旦执行完后,就不再执行后面的else-if了
说明:如果if-else中的语句只有一行,就可以去掉大括号,但是,不建议这样使用。
*/
import java.util.Scanner;
class IfTest {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入成绩:");
		int score = scan.nextInt();
		if(score == 100){
			System.out.println("奖励宝马");
		}else if(80 < score && score <= 99){
			System.out.println("奖励iphone");
		}else if(60 < score && score <= 80){
			System.out.println("奖励ipad");
		}else{                             //也可以没有最后的else
			System.out.println("啥也没有");
		}
	}
}

switch-case:

/*
分支结构之二:switch case
1.格式
switch(表达式){

case 常量1:
	执行语句1;
	//break;

case 常量2:
	执行语句2;
	//break;

case 常量3:
	执行语句3;
	//break;
.
.
.
default:
	执行语句n;
	//break;
}
2.说明:
	根据switch表达式中的值依次匹配各个表达式中的常量,
一旦匹配成功,则进入相应的case中的执行语句。

	当调用完执行语句之后仍然向下继续执行其他case语句,
直到遇见break关键字或者default停止

	switch结构中的表达式,只能是如下的六种数据类型之一:
byte short char int 枚举类型(JDK5.0新增) String类型(JDK7.0新增)

	case之后只能是常量!不能是范围!

	break关键字是可选的。绝大多数情况是需要加的!

	default(默认的) 相当于 if-else中的else,也是可选的!
	
	1.凡是可以使用switch-case的都可以使用if-else,反之不成立。
	2.当写分支结构时,两者都可以使用时,优先使用switch-case,但是表达式的取值情况不太多。
*/

class SwitchCaseTest {
	public static void main(String[] args) {
		int num = 2;
		switch(num){
		case 0:
			System.out.println("zero");
			break;
		case 1:
			System.out.println("one");
			break;
		case 2:
			System.out.println("two");
			break;
		default:
			System.out.println("无");
		}
	}
}

//import java.util.Scanner;
class Test1{
	public static void main(String[] args){
	Scanner scan = new Scanner(System.in);
	String word = scan.next();
	char c = word.charAt(0);
	System.out.println(c);
	switch(c){
	case 'a':
		c = 'A';
		System.out.println(c);
		break;
	case 'b':
		c = 'B';
		System.out.println(c);
		break;
	default:
		System.out.println("输入有误");
	}
	}
}


import java.util.Scanner;
class Test2{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		double c = scan.nextDouble();
		System.out.println(c);
		String result;
		if(c >= 60){
			result = "合格";
		}else{
			result = "不合格";
		}
		switch (result){
		case "合格":
			System.out.println("合格");
			break;
		case "不合格":
			System.out.println("合格");
			break;
		default:
			System.out.println("格式错误");
		}
	}
}


class Test3{
	public static void main(String[] args){
		int month = 7;
		switch (month){
		case 3:
		case 4:
		case 5:
			System.out.println("春季");
			break;
		case 6:
		case 7:
		case 8:
			System.out.println("夏季");
			break;
		case 9:
		case 10:
		case 11:
			System.out.println("秋季");
			break;
		case 12:
		case 1:
		case 2:
			System.out.println("冬季");
			break;
		default:
			System.out.println("你的输入有误!");
		}
	}
}


import java.util.Scanner;
class Test4{
	public static void main(String[] args){
	Scanner scan = new Scanner(System.in);
	int year = scan.nextInt();
	int month = scan.nextInt();
	int day = scan.nextInt();
	System.out.println("您输入的是" + year + "年" + month + "月" + day +"日");
	//定义一个变量来计算总天数
	int sumDays = 0;
	switch (month){
	case 12:
		sumDays += 30;
	case 11:
		sumDays += 31;
	case 10:
		sumDays += 30;
	case 9:
		sumDays += 31;
	case 8:
		sumDays += 31;
	case 7:
		sumDays += 30;
	case 6:
		sumDays += 31;
	case 5:
		sumDays += 28;
	case 4:
		sumDays += 28;
	case 3:
		//判断年份是不是闰年:
		if((year % 4 == 0 && year % 100 == 0) || year % 400 == 0){
			sumDays += 29;		
		}else{
			sumDays += 28;		
		}
	case 2:
		sumDays += 31;
	case 1:
		sumDays += day;
	}
	System.out.println(year + "年" + month + "月" + day +"日是当年的第" + sumDays + "天");
	}
}

版权声明:本文为m0_63104578原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/m0_63104578/article/details/124564206