public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); //创建扫描键盘输入对象
boolean flag = true;
dowhile:
while (flag) {
//循环下列语句
System.out.println("1:查看年月天数");
System.out.println("2:99乘法表");
System.out.println("3:鸡兔同笼");
System.out.println("4:根据输入的数判断是基数还是偶数");
System.out.println("9:退出系统");
System.out.println("请选择:");
int input = scanner.nextInt();
switch (input) {
//1的方法
case 1:
System.out.println("请输入年份:");
int year = scanner.nextInt();
System.out.println("请输入月份:");
int month = scanner.nextInt();
getDay(year, month);
break;
//2的方法
case 2:
nineNineTable();
break;
//3的方法
case 3:
System.out.println("请输入脚的数量:");
int leg = scanner.nextInt();
System.out.println("请输入头的数量:");
int head = scanner.nextInt();
caiXuKun(leg, head);
break;
//4的方法
case 4:
System.out.println("请输入数字:");
int number = scanner.nextInt();
jiOu(number);
break;
//9的方法
case 9:
System.out.println("程序结束");
return;//结束循环
}
}
}
//判断天数的方法
public static void getDay(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("天数为:31天");
break;
case 2:
//判断闰年
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println("天数为:29天");
} else {
System.out.println("天数为:28天");
}
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("天数为:30天");
break;
default:
System.out.println("输入月份错误!");
break;
}
}
//九九乘法表的方法
public static void nineNineTable(){
for (int i=1;i<=9;i++){
for (int j=1;j<=i;j++){
System.out.print(i+"*"+j+"="+i*j+" ");
}
System.out.println();
}
}
//鸡兔同笼的方法
public static void caiXuKun(int head,int leg){
for (int kun=0;kun>=0;kun++) {
int rabbit = head - kun;
if (kun + rabbit == head && 2 * kun + 4 * rabbit == leg) {
System.out.println("坤的总数为:" + kun + " " + "兔子的总数为:" + rabbit);
} else {
System.out.println("脚或头的数量错误,请重新输入!");
}
break;
}
}
//鸡偶数的方法
public static void jiOu(int number){
if (number%2==0){
System.out.println("这个数是偶数");
}else {
System.out.println("这个数是奇数");
}
}
前几次代码合为一,且各个功能分装成方法
最新推荐文章于 2024-10-18 13:02:57 发布