因为公元没有0,所以只要把负数都+1,符合正常数字的规律就可以解决当公元年为负数时,负数模12等于0与正数模12等于0不匹配的问题了.希望可以帮助到你!- import java.util.Scanner;
- class ShengXiao1{
- public static void main(String[] args){
- System.out.println("请输入一个年份");
- Scanner x=new Scanner(System.in);
- int n=x.nextInt();
- if (n<=0) {
- n+=1;
- }
- switch(n % 12){
- case -11:
- case 1:
- print(n);
- System.out.println("年是鸡年");
- break;
- case -10:
- case 2:
- print(n);
- System.out.println("年是狗年");
- break;
- case -9:
- case 3:
- print(n);
- System.out.println("年是猪年");
- break;
- case -8:
- case 4:
- print(n);
- System.out.println("年是鼠年");
- break;
- case -7:
- case 5:
- print(n);
- System.out.println("年是牛年");
- break;
- case -6:
- case 6:
- print(n);
- System.out.println("年是虎年");
- break;
- case -5:
- case 7:
- print(n);
- System.out.println("年是兔年");
- break;
- case -4:
- case 8:
- print(n);
- System.out.println("年是龙年");
- break;
- case -3:
- case 9:
- print(n);
- System.out.println("年是蛇年");
- break;
- case -2:
- case 10:
- print(n);
- System.out.println("年是马年");
- break;
- case -1:
- case 11:
- print(n);
- System.out.println("年是羊年");
- break;
- case 0:
- print(n);
- System.out.println("年是猴年");
- break;
-
- default:
- System.out.println("您输入的数据有误!");
- break;
- }
- }
- public static void print(int n){
- if (n<=0) {
- n-=1;
- }
- System.out.print(n);
-
- }
- }
复制代码 |