The first type: switch...case
publicstaticvoidmain(String[] args) { Scanner sc=new Scanner(System.in);//console input System.out.println("Please enter the year you are looking for:");//hint int year=sc.nextInt()%12;//The remainder obtained after the input year and the remainder of 12 switch(year) { case0: System.out.println("This year is the year of the monkey"); break; case1: System.out.println("this year is the year of the rooster"); break; case2: System.out.println("this year is the year of the dog"); break; case3: System.out.println("this year is the year of the pig"); break; case4: System.out.println("this year is the year of the rat"); break; case5: System.out.println("this year is the year of the ox"); break; case6: System.out.println("this year is the year of the tiger"); break; case7: System.out.println("this year is the year of the rabbit"); break; case8: System.out.println("this year is the year of the dragon"); break; case9: System.out.println("This year is the year of the snake"); break; case10: System.out.println("This year is the Year of the Horse"); break; case11: System.out.println("this year is the year of the sheep"); break; default: System.out.println("illegal input"); } }
① First create a main method;
②Call the Scanner class to input the year in the console;
③ Write an output statement to prompt the user to input;
④ Calculate the remainder of the input year and 12; (because the reincarnation of a zodiac sign is 12 years)
⑤ Then use the switch...case statement to judge, case 0 is the year of the monkey, case 1 is the year of the rooster, and so on. (Because according to history, AD 1 is the Year of the Rooster, so it can be deduced that the year before AD 1 is the Year of the Monkey)
Little knowledge: AD 1, also known as the first year of AD, happened to be the first year of the last emperor of the Western Han Dynasty, Emperor Hanping. This year is the year of Xinyou, that is, the year of the rooster.
! ! ! Key point: A break function must be added at the end of each case statement, otherwise the code will penetrate downwards, not only the output result is the calculated year, but also the year after this year will be output together.

⑥ Add default at the end of the judgment to judge the situation of abnormal input. (default in switch...case is used to judge the statement to be executed when all the statements in the switch statement are not true)

output result

The second type: if...else...else if...else
package com.gec.practice; import java.util.Scanner; publicclassTwo { publicstaticvoidmain(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Please enter the year:"); int years=sc.nextInt(); if(years<=0) { System.out.println("Illegal input..."); }elseif(years%12==0) { System.out.println("This year is the year of the monkey"); }elseif(years%12==1) { System.out.println("this year is the year of the rooster"); }elseif(years%12==2) { System.out.println("this year is the year of the dog"); }elseif(years%12==3) { System.out.println("this year is the year of the pig"); }elseif(years%12==4) { System.out.println("this year is the year of the rat"); }elseif(years%12==5) { System.out.println("this year is the year of the ox"); }elseif(years%12==6) { System.out.println("this year is the year of the tiger"); }elseif(years%12==7) { System.out.println("this year is the year of the rabbit"); }elseif(years%12==8) { System.out.println("this year is the year of the dragon"); }elseif(years%12==9) { System.out.println("This year is the year of the snake"); }elseif(years%12==10) { System.out.println("This year is the Year of the Horse"); }elseif(years%12==11) { System.out.println("this year is the year of the sheep"); } } }
① First create a main method;
②Call the Scanner class to input the year in the console;
③ Write an output statement to prompt the user to input;
④ First judge whether the input year is legal, if not, jump out, and continue to judge if it is legal.

⑤ Calculate the remainder of the input year and 12; (because the reincarnation of a zodiac sign is 12 years)
⑥If the obtained remainder is 1, it is determined that this year is the year of the rooster; from this, it can be deduced that if the obtained remainder is 0, it is determined that this year is the year of the monkey; if the obtained remainder is 2, it is determined that this year is the year of the dog... and so on

Welcome everyone to comment and interact~_~