练习
1.60分以上的人及格,60分以下不及格。
package com.xin.base;
public class Practice07
{
//60分以上的人及格,因为switch语句case里不能比较
public void compare(){
int score = 88;
switch (score / 10){//除以10可以得到十位数,又因为是int型没有小数点
case 1:
case 2:
case 3:
case 4:
case 5://重复的语句在switch只可以省略
System.out.println("不及格");
break;
case 6:
case 7:
case 8:
case 9:
case 10:
System.out.println("及格");
break;
}
//这样太复杂我们也可以直接除60
int score1 = 60;
switch(score / 60){
case 0 :
System.out.println("不及格");
break;
case 1 :
System.out.println("及格");
break;
}
}
练习2
从键盘分别输入年、 月、日,判断这一天是当年的第几天
主:判断一年是否是闰年的标准:
1)可以被4整除,但不可被100整除或
2)可以被400整除
package com.xin.base;
import java.util.Scanner;
import com.xin.base.*;
/*
*/
public class Practice5
{
public static void main (String[] args){
Scanner input = new Scanner(System.in);
//从键盘分别输入年、 月、日,判断这一天是当年的第几天
//主:判断一年是否是闰年的标准:
//1)可以被4整除,但不可被100整除或
//2)可以被400整除
//berak在把case倒起来的时候可以不加
System.out.println("请输入年");
int year = input.nextInt();
System.out.println("请输入月");
int month = input.nextInt();
System.out.println("请输入日");
int day = input.nextInt();
//定义一个变量储存月份的天数
int sumDay = 0;
switch(month){
case 12: //每月都加上个月的天数
sumDay += 30;
case 11:
sumDay += 31;
case 10:
sumDay += 30;
case 9:
sumDay += 31;
case 8:
sumDay += 31;
case 7:
sumDay += 30;
case 6:
sumDay += 31;
case 5:
sumDay += 30;
case 4:
sumDay += 31;
case 3://因为判断闰年所以我们加个if
if(year % 4 == 0 && year % 100 != 0 || year % 100 ==0){
sumDay += 29;
}else {
sumDay += 28;
}
case 2:
sumDay += 31;
case 1:
sumDay += day;
}
System.out.println("你输入的是今年"+year+"的第"+month+"个月"+day+"天");
System.out.println("是今年的第"+sumDay+"天");
input.close();
}
}
练习3
12年为生肖一轮回,请输出一个输入年份算出今年是什么生肖。
public class Practice5
{
public static void main (String[] args){
System.out.println("请输入年份");
Scanner input = new Scanner(System.in);
//用户输入一个年份,然后计算出该年是十二生肖中的哪一年
//提示 2019 年猪年 2019 % 12 == 3
//鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪
int year = input.nextInt();
switch(year % 12 ){
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("兔");
break;
case 8 :
System.out.println("龙");
break;
case 9 :
System.out.println("蛇");
break;
case 10 :
System.out.println("马");
break;
case 11 :
System.out.println("羊");
break;
case 12 :
System.out.println("猴");
break;
}
input.close();
}
}