java学习第四天

本文详细讲解了如何在Java中使用switch和if语句进行月份天数计算,并通过实例演示了英雄技能释放的逻辑。同时介绍了简化代码的方法,涉及字符输入验证、计算器运算和段位判定等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

if的格式三:

/*  	if(条件语句1){
* 		//代码块1
* 		}else if(条件语句2){
* 		//代码块2
* 		}else if(条件语句3){
* 		//代码块3
* 		}
* 		-----
* 		else{
* 		//代码块
* 		}
* /

键盘扫描器不能接收字符,只能接收字符串
比较字符串,不能使用==,要使用 变量名.equalsIgnoreCase(字符串) 变量名.equals(字符串)

	/*
	英雄释放技能
	*/
	public static void main(String[] args) {
		String word;
		Scanner key=new Scanner(System.in);
		System.out.println("按 Q W E R 释放技能");
		String con=key.next();
		if (con.equalsIgnoreCase("Q")) {
			word="致命打击!";
		}else if(con.equalsIgnoreCase("W")) {
			word="勇气!";
		}else if(con.equalsIgnoreCase("E")) {
			word="审判!";
		}else if(con.equalsIgnoreCase("R")) {
			word="德玛西亚正义!";
		}else {
			word="不要乱按!";
		}
		System.out.println(word);
		key.close();
	}

小测试:

  1. 控制台输入年份和月份
  2. 输出 这一年 这一个月一共有多少天
    输入:2019年 8月
    输出:2019年8月一共有31天
    使用if语句编写
	//从年上进行判断
	public static void main(String[] args) {
		int month=0;
		
		//定义扫描器
		Scanner key=new Scanner(System.in);
		//提示输入年份
		System.out.println("请输入年份");
		int myYear=key.nextInt();
		System.out.println("请输入月份");
		int myMonth=key.nextInt();
		
		if(myYear%4==0&&myYear%100==0||myYear%400==0) { 
			if(myMonth==2) {
				month=29;
			}else if(myMonth==1||myMonth==3||myMonth==5||myMonth==7||myMonth==8||myMonth==10||myMonth==12){
				month=31;
			}else {
				month=30;
			}
			
		}else{
			if(myMonth==2) {
				month=28;
			}else if(myMonth==1||myMonth==3||myMonth==5||myMonth==7||myMonth==8||myMonth==10||myMonth==12){
				month=31;
			}else {
				month=30;
			}
		}
		System.out.println(myYear+"年"+myMonth+"月有"+month+"天");
		key.close();
	}

建议写下面:代码重复少

	//从月份上进行判断
	public static void main(String[] args) {
		int month=0;
		
		//定义扫描器
		Scanner key=new Scanner(System.in);
		//提示输入年份
		System.out.println("请输入年份");
		int myYear=key.nextInt();
		System.out.println("请输入月份");
		int myMonth=key.nextInt();
		if(myMonth==1||myMonth==3||myMonth==5||myMonth==7||myMonth==8||myMonth==10||myMonth==12) {
			month=31;
		}else if(myMonth==4||myMonth==6||myMonth==9||myMonth==11) {
			month=30;
		}else if(myMonth==2) {
			if(myYear%4==0&&myYear%100==0||myYear%400==0) {
				month=29;
			}else {
				month=28;
			}
		}else {
			System.out.println("请输入正确的月份");
		}
		System.out.println(myYear+"年"+myMonth+"月有"+month+"天");
		key.close();
	}

小作业:
简易计算器
输入两个数,输入一个运算符,根据运算符做出相应的运算
比如:输入 3 , 4 ,+
输出:3+4=7

	public static void main(String[] args) {
		double sum=0;
		Scanner key=new Scanner(System.in);
		System.out.println("请输入第一个数");
		double number1=key.nextDouble();
		System.out.println("请输入第二个数");
		double number2=key.nextDouble();
		System.out.println("请输入+ - * / ");
		String operator=key.next();
		if(operator.equals("+")) {
			sum=number1+number2;
		}else if(operator.equals("-")) {
			sum=number1-number2;
		}else if(operator.equals("*")) {
			sum=number1*number2;
		}else if(operator.equals("/")) {
			sum=number1/number2;
		}else {
			System.out.println("请输入正确的运算符号");
		}
		System.out.println(number1+operator+number2+"="+sum);
		key.close();
		
	}

java中的分支语句-switch
格式:

switch(){
	case 1:代码块1;
	case 2:代码块2;
	case 3:代码块3;
	------
	defacult:代码块
}

表示:会根据switch括号中值去匹配case中的值,一旦匹配,就会从当前对应代码块开始一直往下执行,直到遇到break或switch结束,如果匹配不到值,就会执行default中的代码

public static void main(String[] args) {
		int num=2;
		switch (num) {
		case 1:
			System.out.println(1);
		case 2:
			System.out.println(2);
		case 3:
			System.out.println(3);
		default:
			System.out.println(4);
			
		}
	}

在这里插入图片描述
小面试题:
switch和if的区别,什么时候用switch,什么时候用if
答:if括号中是条件语句,是boolean类型的,可以是个范围,switch括号中是一个具体的值.

  1. 知道某个条件,或范围是,一般用if

  2. 需要根据某个值去匹配其他值时,(多个固定值,)用switch
    switch括号中可以放哪些类型的值?
    答:只能放byte, short ,char, int ,String, 枚举 ,注意在jdk1.7以前,String是不能放的
    switch里面的case有没有顺序限制吗?
    答:写了break没有限制,
    小测试:

  3. 控制台输入年份和月份

  4. 输出 这一年 这一个月一共有多少天
    输入:2019年 8月
    输出:2019年8月一共有31天
    使用switch语句编写

public static void main(String[] args) {
		int day=0;
		Scanner key=new Scanner(System.in);
		System.out.println("请输入年份");
		int year=key.nextInt();
		System.out.println("请输入月份");
		int month=key.nextInt();
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day=31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			day=30;
			break;
		default:
				
		}
		if(year%4==0&&year%100==0||year%400==0){
			day=29;
		}else {
			day=28;
		}
		System.out.println(year+"年"+month+"月有"+day+"天");
		key.close();
	}

小作业:
积分转换段位
段位判定
50分以下 倔强青铜
50-60 秩序白银
60-70 黄金
70-80 铂金
80-90 钻石
90-100 星耀
100以上 王者

	public static void main(String[] args) {
		String word;
		Scanner key=new Scanner(System.in);
		System.out.println("请输入你的积分");
		int number=key.nextInt();
		switch (number/10) {
		case 1:
		case 2:
		case 3:
		case 4:
			word="倔强青铜";
			break;
		case 5:
			word="秩序白银";
			break;
		case 6:
			word="黄金";
			break;
		case 7:
			word="铂金";
			break;
		case 8:
			word="钻石";
			break;
		case 9:
			word="星耀";
			break;
		default:
			word="王者";
			break;
		}
		System.out.println("你的段位是:"+word);
		key.close();
	}

java中的循环语句-while
循环:重复做某件事
格式:

	while(条件表达式){
	   代码块
	}

表示:只要条件表达式成立,就执行代码块, 直到条件表达式不成立才结束循环
小笔试:

	public static void main(String[] args) {
		while(true) {
			System.out.println("恭喜发财!");
		}
		System.out.println("-_-!");//不可执行的代码语句
	}

在这里插入图片描述

	public static void main(String[] args) {
		int num=6;
		while(num<8) {
			System.out.println("恭喜发财!");
		}
		System.out.println("-_-!");
	}

在这里插入图片描述
从上面两张图可以看出得到:编译器只管到编译期时的代码,true是字面量,它在编译期就可以确定, n是变量,它的值是在运行期时确定,在编译期时,编译器没法获取n的值,从而就没办法判断n是否小于8,所以无法判断是否是死循环。

弹窗小炸弹:10个弹窗

	public static void main(String[] args) {
		Random rd=new Random();
		int n=0;
		while(n<10) {//循环10次
			//创建窗体
			JFrame frame=new JFrame();
			//设置窗体的大小
			frame.setSize(400,100);
			//设置窗体的位置
			frame.setLocation(rd.nextInt(1900),rd.nextInt(1000));
			//显示窗体
			frame.setVisible(true);
			n++;
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值