day_004

import java.util.Scanner;
class Test1_If {

	public static void main(String[] args) {
		/*
		
		练习:键盘录入成绩,判断优良
		*/
	
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入学生成绩范围在1到100之间");

		int x = sc .nextInt();

		if (x >=90 && x <= 100) {
			System.out.println("优");
		
		}else if (x >= 80 && x <=89) {
			System.out.println("良");
		
		}else if (x >= 70 && x <=79) {
			System.out.println("中");
		
		}else if (x >= 60 && x <=69) {
			System.out.println("及");
		
		}else if (x >=0 && x <=59) {
			System.out.println("差");
		
		}else
			System.out.println("成绩录入错误");

	}
}

if语句的小练习

import java.util.Scanner;
class Test1_If {

	public static void main(String[] args) {
		/*
		
		练习:键盘录入成绩,判断优良
		*/
	
		Scanner sc = new Scanner(System.in);
		/*System.out.println("请输入学生成绩范围在1到100之间");

		int x = sc .nextInt();

		if (x >=90 && x <= 100) {
			System.out.println("优");
		
		}else if (x >= 80 && x <=89) {
			System.out.println("良");
		
		}else if (x >= 70 && x <=79) {
			System.out.println("中");
		
		}else if (x >= 60 && x <=69) {
			System.out.println("及");
		
		}else if (x >=0 && x <=59) {
			System.out.println("差");
		
		}else
			System.out.println("成绩录入错误");
		
		System.out.println();
		*/
		System.out.println("请输入第一个整数");
		int x = sc.nextInt();
		int y =0;
		if (x>= 3) {
			y=2*x+1;
		}else if (x>=-1 && x<3) {
			y =2*x;

		}else if (x<=-1) {
			y =2*x-1;
		}
		System.out.println(y);
	}
	
}

用if语句嵌套比较三个数的大小

class Demo6_IfIf {

	public static void main(String[] args) {
		int a = 10;
		int b = 20;
		int c = 30;
		if (a>b) {
			if (a>c) {
				System.out.println(a+"是最大值");
			
			}else {
				System.out.println(c+"是最大值");
			
			}
		
		}else {
			if (b > c) {
				System.out.println(b+"是最大值");
			}else {
				System.out.println(c+"是最大值");
			}
		
		}

	
		
	}
}

switch语句格式:

class Demo_Switch {
	/*seitch语句格式
	switch(表达式){    可以接受byte,short,,char,int
		case 值一;       引用数据类型可以接受枚举,string字符串
		语句体1;
		break;
			case 值2;
			break;
			。。。。
			default ;
			语句体n+1
		}
		*/

	public static void main(String[] args) {
		String name ="rose";
		String gender ="跨性别认知者";
		switch (gender) {
		case "男士":
			System.out.println(name + "是一位" + gender + "喜欢抽烟喝酒抠脚");
		break;
		case "女士":
			System.out.println(name + "是一位" + gender + "喜欢逛街看剧看小说");
		break;
		default:
			System.out.println(name + "是一位" + gender + "他战胜世俗眼光做自己");
		break;

		}

	}
}

 

switch语句的小练习

class Test1_Switch {

	public static void main(String[] args) {
		//给出一个值,输出对应的星期几
		int week =5;
		switch (week) {
		case 1:
		System.out.println("星期一");
		break;
		case 2:
		System.out.println("星期二");
		break;
		case 3:
		System.out.println("星期三");
		break;
		case 4:
		System.out.println("星期四");
		break;
		case 5:
		System.out.println("星期五");
		break;
		case 6:
		System.out.println("星期六");
		break;
		case 7:
		System.out.println("星期天");
		default:
		System.out.println("输入无效数字");
		}
	
	}
}

switch语句注意事项:

case后面只能是常量,不能是变量,多个case后面的值不能出现相同

default可以省略但一般不建议省略。

break,最后一个可以省略,其他不可以省略,省略可能导致case穿透的情况;

case后面只能放byte short char int 以及string字符串和enum枚举

default可以放在任意位置,但是一般放在最后,不管放在哪里都是最后执行。

switch的结束条件

1.遇到break

2.遇到右大括号

switch语句if语句适用范围

if建议在某个区间做判断

switch建议对固定值做判断

import java.util.Scanner;
class Test3_Switch {

	public static void main(String[] args) {
		/*一年有四季,
		345对应春季,
		678夏季
		9.10.11秋季
		12.1.2冬季

		*/
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入月份");
		int month = sc.nextInt();
		/*switch (month) {
		case 3:
		case 4:
		case 5:
			System.out.println(month+"月是春天");
		break;

		case 6:
		case 7:
		case 8:
			System.out.println(month+"月是夏天");
		break;
		case 9:
		case 10:
		case 11:
			System.out.println(month+"月是秋天");
		break;
		case 12:
		case 1:
		case 2:
			System.out.println(month+"月是冬天");
		break;
		default:
			System.out.println("对8起,没有对应的季节");
			*/
			
			if(month>12 || month<1){
				System.out.println("对不起没有对应的季节");
			}else if(month>=3 && month <=5){
				System.out.println(month+"月是春季");
			}else if(month>=6 && month<=8) {

				System.out.println(month+"月是夏季");
			}else if(month>=9 && month<=11){
				System.out.println(month+"月是秋季");
			}else {
				System.out.println(month+"月是冬季");
			
			}
		
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值