Java實現輸入年份,輸出對應的生肖年

摘要:
  十二生肖的排序:鼠、牛、虎、兔、龍、蛇、馬、羊、猴、雞、狗、豬
提示:
  可以利用switch語句實現
語法:

 switch(表達式){
     case1:
       表達式的值和 值1匹配上了,需要執行的代碼;
       break;
     case2:
       表達式的值和 值2匹配上了,需要執行的代碼;
     break;
     case3:
       表達式的值和 值3匹配上了,需要執行的代碼;
     break;
     default:
       如果表達式的值和以上的case後面的值都沒有匹配上,那麼就執行這裏的代碼。
       break;
   }
//不寫break會穿透到下一個break

參考代碼1:(推薦)

package com.gx.demo;

import java.util.Scanner;

public class Test1 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);// 聲明掃描儀變量
		System.out.println("請輸入年份");// 系統提示輸入
		try { // 監聽異常
			while (true) {
				int birth = input.nextInt() % 12; // 用戶輸入%12在再轉換
				switch (birth) {
				case 0:
					System.out.println("猴年");
					break;
				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;
				default:
					System.out.println("錯誤!請輸入大於0的數"); // 不滿足以上條件的默認輸出這個語句
				}
			}
		} catch (Exception e) { // 捕捉異常
			System.out.println("請正確輸入");
			e.printStackTrace(); // 打印異常信息在程序中出錯的位置及原因
		}
	}
}

輸出結果:
例:

請輸入年份
1998
虎年

解釋:
  公元4年是甲子年,也就是屬鼠;每隔12年一個循環,所以用年%12取餘數判斷即可;那麼已知餘數爲4是屬鼠(公元4年%12爲4),依次類推5爲醜,6爲寅,7爲卯,8爲辰,9爲巳,10爲午,11爲未,12(即0)爲申,1爲酉,2爲戌,3爲亥。

參考代碼2:(不推薦)

package com.gx.demo;

import java.util.Scanner;

public class Test2 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);// 聲明掃描儀變量
		System.out.println("請輸入年份");// 系統提示輸入
		try { // 監聽異常
			while (true) {
				int birth = (input.nextInt()-4) % 12; // 用戶輸入-4然後%12再轉換(按生肖順序來)
				switch (birth) {
				case 0:
					System.out.println("鼠年");
					break;
				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;
				default:
					System.out.println("錯誤!請輸入大於0的數"); // 不滿足以上條件的默認輸出這個語句
				}
			}
		} catch (Exception e) { // 捕捉異常
			System.out.println("請正確輸入");
			e.printStackTrace(); // 打印異常信息在程序中出錯的位置及原因
		}
	}
}

輸出結果:
例:

請輸入年份
1997
牛年

解釋:
  年份除於12,得出的餘數減去3,所得的數字(如果所得數字爲負數,加上12)就是相對應12生肖;12生肖順序爲: 1.鼠 2.牛 3.虎 4.兔 5.龍 6.蛇 7.馬 8.羊 9.猴 10.雞 11.狗 12.豬;比如1997%12,餘數爲5;5-3=2,2就對應生肖排序中的牛啦。
PS:其實這種推算也是根據規律來的,知道就好了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章