switch case的练习

switch case


1、一年中有12个月,而每个月的天数是不一样的,其中,有7个月为31天
称为大月,分别为1,3,5,7,8,10,12月,有4个月为30天,称为小月,分别
为4,6,9,11月,还有二月比较特殊,平年的二月只有28天,而闰年的二月
有29天,由用户在控制台输入年份和月份,程序计算该年该月的天数

2、用户从控制台输入大(1)小(0),系统随机生成1-10之间的整数
根据规则用户是否猜对大小
游戏规则:
用户输入0表示小,1表示大
1-5为小
6-10为大
如果用户赢则输出你猜对了,否则输出请继续努力

第一个:
package a0717;

import java.util.Scanner;

public class a {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(“请你输入年份”);
int year = scan.nextInt();
System.out.println(“请你输入月份”);
int month = scan.nextInt();
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(year+“年”+month+“月有”+31+“天”);
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(year+“年”+month+“月有”+30+“天”);
break;
case 2:
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
System.out.println(year+“年”+month+“月有”+29+“天”+" 并且该年是闰年");
}else{
System.out.println(year+“年”+month+“月有”+28+“天”+" 并且该年是平年");
}
break;
default :{
System.out.println(“请输入正确的月份”);
}
}
}
}

swith case里遇到break就会停止

第二个
package a0717;

import java.util.Random;
import java.util.Scanner;

public class a3 {

/**
 * @param args
 */
public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner scan = new Scanner(System.in);
	Random random = new Random();
	System.out.println("请输入大(1)————小(0)");
	int sj = random.nextInt(10)+1;
	int input = scan.nextInt();
	System.out.println("你输入的数为"+input+"   "+"系统生成的数为"+sj);
	switch(input){
	case 0:
		if(sj>=0||sj<=5){
			System.out.println("你猜对了,恭喜");
		}else{
			System.out.println("你猜错了,恭喜");
		}
	break;
	case 1:
		if(sj>=6||sj<=10){
			System.out.println("你猜对了,恭喜");
		}else{
			System.out.println("你猜错了,恭喜");
		}
	break;
	default :{
		System.out.println("请输入正确的数字");
	}
	}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值