【发布时间】:2021-01-21 16:32:52
【问题描述】:
我希望并寻找其他一些想法或方法来做到这一点。如您所见,我要求用户输入 mmddyyyy(05022001 = 2001 年 5 月 2 日)并使用子字符串以便我在索引中获取特定输入然后解析它。现在,我想要完成的是找到另一种方法来做到这一点,例如使用正式的日期格式化程序,在我的情况下我没有使用。我认为日期格式化程序比这要好得多,但我不明白它的想法。
这是我到目前为止所得到的。提前致谢。
import java.util.*;
public class ZodiaChineseBirthSign {
public static void main(String [] args) {
Scanner reader = new Scanner(System.in);
String mm,dd,yyyy;
int mm1,dd1,yyyy1;
/**
* To keep asking for the input from the user I have to use looping
* which is the while loop.
*
*/
while(true) {
System.out.print("Enter your birthdate: ");
String birth = reader.next();
//Substring is to get the specific input of the user.
mm = birth.substring(0, 2);
dd = birth.substring(2, 4);
yyyy = birth.substring(4);
//ParseInt which is used to convert String into Integer in order for me to be able to determine the Animal Sign and Zodiac.
mm1 = Integer.parseInt(mm);
dd1 = Integer.parseInt(dd);
yyyy1 = Integer.parseInt(yyyy);
try {
if (mm1 < 1 || mm1 > 12 || dd1 < 1 || dd1 > 31) {
throw new ArrayIndexOutOfBoundsException();
}
switch (mm1) {
case 1: System.out.println("You were born on January " + dd1 + ", " + yyyy1 ); break;
case 2: System.out.println("You were born on February " + dd1 + ", " + yyyy1 ); break;
case 3: System.out.println("You were born on March " + dd1 + ", " + yyyy1 ); break;
case 4: System.out.println("You were born on April " + dd1 + ", " + yyyy1 ); break;
case 5: System.out.println("You were born on May " + dd1 + ", " + yyyy1 ); break;
case 6: System.out.println("You were born on June " + dd1 + ", " + yyyy1 ); break;
case 7: System.out.println("You were born on July " + dd1 + ", " + yyyy1 ); break;
case 8: System.out.println("You were born on August " + dd1 + ", " + yyyy1 ); break;
case 9: System.out.println("You were born on September " + dd1 + ", " + yyyy1 ); break;
case 10: System.out.println("You were born on October " + dd1 + ", " + yyyy1 ); break;
case 11: System.out.println("You were born on November " + dd1 + ", " + yyyy1 ); break;
case 12: System.out.println("You were born on December " + dd1 + ", " + yyyy1 ); break;
}
switch (yyyy1 % 12) {
case 0: System.out.println("in the Year of the Monkey"); break;
case 1: System.out.println("in the Year of the Rooster"); break;
case 2: System.out.println("in the Year of the Dog"); break;
case 3: System.out.println("in the Year of the Pig"); break;
case 4: System.out.println("in the Year of the Rat"); break;
case 5: System.out.println("in the Year of the Ox"); break;
case 6: System.out.println("in the Year of the Tiger"); break;
case 7: System.out.println("in the Year of the Rabbit"); break;
case 8: System.out.println("in the Year of the Dragon"); break;
case 9: System.out.println("in the Year of the Snake"); break;
case 10: System.out.println("in the Year of the Horse"); break;
case 11: System.out.println("in the Year of the Sheep"); break;
}
if ((dd1>21 && mm1 == 12) || (dd1<=19 && mm1== 1)){
System.out.println("under the sign of Capricorn");
}
if ((dd1>20 && mm1 ==1) || (dd1<=18 && mm1 == 2)){
System.out.println("under the sign of Aquarius");
}
if ((dd1>19 && mm1 == 2) || (dd1 <=20 && mm1 == 3)){
System.out.println("under the sign of Pisces");
}
if ((dd1>21 && mm1 == 3) || (dd1 <=20 && mm1 == 4)){
System.out.println("under the sign of Aries");
}
if ((dd1>21 && mm1 == 4) || (dd1 <=20 && mm1 == 5)){
System.out.println("under the sign of Taurus");
}
if ((dd1>21 && mm1 == 5) || (dd1<=20 && mm1 == 6)){
System.out.println("under the sign of Gemini");
}
if ((dd1>21 && mm1 == 6) || (dd1<=20 && mm1 == 7)){
System.out.println("under the sign of Cancer");
}
if ((dd1>21 && mm1 == 7) || (dd1<=20 && mm1== 8)){
System.out.println("under the sign of Leo");
}
if ((dd1>21 && mm1 ==8) || (dd1<=22 && mm1 == 9)){
System.out.println("under the sign of Virgo");
}
if ((dd1>23 && mm1 == 9) || (dd1<=20 && mm1 ==10)){
System.out.println("under the sign of Libra");
}
if ((dd1>21 && mm1 == 10) || (dd1<=22 && mm1 == 11)){
System.out.println("under the sign of Scorpio");
}
if ((dd1>23 && mm1 == 11) || (dd1<=20 && mm1 == 12)){
System.out.println("under the sign of Sagittarius");
}
}
catch (ArrayIndexOutOfBoundsException e) { //Catch-Block which will print(enter correct birthdate) whenever it throws exception error.
System.out.println("Please enter the correct Birthdate");
}
System.out.println("");
}
/**
* Thanks to some of my classmate for giving me some snippets of codes and ideas ^_^
*/
}
}
【问题讨论】:
标签: java switch-statement try-catch