星座项目(需要帮助!)

时间:2014-10-19 21:50:10

标签: int project

我看到问题是int MATHDATE不会像它应该的那样改变。每次我进入生日时,标志都会出现为摩羯座,因为MATHDATE不会从0改变。有人可以帮我解决这个问题吗?谢谢。

System.out.println("Please enter your birthday in mm/dd format: ");

   Scanner birthdayin = new Scanner(System.in);
   String bmonth = scan.nextLine();
   int bday = Integer.parseInt((bmonth.substring(0,bmonth.indexOf('/'))));
   String horoscope = null;
   int MATHDATE = 0;

   if (bmonth.equals("01") || bmonth.equals("1")){
   MATHDATE = 0 + bday;}
   if (bmonth.equals("02") || bmonth.equals("2")){
   MATHDATE = 31 + bday;}
   if (bmonth.equals("03") || bmonth.equals("3")){
   MATHDATE = 59 + bday;}
   if (bmonth.equals("04") || bmonth.equals("4")){
   MATHDATE = 90 + bday;}
   if (bmonth.equals("05") || bmonth.equals("5")){
   MATHDATE = 120 + bday;}
   if (bmonth.equals("06") || bmonth.equals("6")){
   MATHDATE = 151 + bday;}
   if (bmonth.equals("07") || bmonth.equals("7")){
   MATHDATE = (181 + bday);}
   if (bmonth.equals("08") || bmonth.equals("8")){
   MATHDATE = 212 + bday;}
   if (bmonth.equals("09") || bmonth.equals("9")){
   MATHDATE = 243 + bday;}
   if (bmonth.equals("10")){
   MATHDATE = 273 + bday;}
   if (bmonth.equals("11")){
   MATHDATE = 304 + bday;}
   if (bmonth.equals("12")){
   MATHDATE = 334 + bday;}

   if (MATHDATE >= 20 && MATHDATE <= 49)
   {horoscope = ("AQUARIUS");}
   else if (MATHDATE >= 50 && MATHDATE <= 79)
   {horoscope = ("PISCES");}
   else if (MATHDATE >=80 && MATHDATE <=110)
   {horoscope = ("ARIES");}
   else if (MATHDATE >= 111 && MATHDATE <= 141)
   {horoscope = ("TAURUS");}
   else if (MATHDATE >= 142 && MATHDATE <= 172)
   {horoscope = ("GEMINI");}
   else if (MATHDATE >= 173 && MATHDATE <= 203)
   {horoscope = ("CANCER");}
   else if (MATHDATE >= 204 && MATHDATE <= 234)
   {horoscope = ("LEO");}
   else if (MATHDATE >= 235 && MATHDATE <= 265)
   {horoscope = ("VIRGO");}
   else if (MATHDATE >= 266 && MATHDATE <= 295)
   {horoscope = ("LIBRA");}
   else if (MATHDATE >= 296 && MATHDATE <= 325)
   {horoscope = ("SCORPIO");}
   else if (MATHDATE >= 326 && MATHDATE <= 355)
   {horoscope = ("SAGITTARIUS");}
   else if (MATHDATE >= 356 || MATHDATE<= 19)
   {horoscope = ("CAPRICORN");}


   System.out.println("Your sign is: " + horoscope);

2 个答案:

答案 0 :(得分:0)

如果某人确实以该格式输入了他们的生日(例如,“12/15”),bmonth将是“12/15”(这与您的任何条件都不符),因为您不知道在调用scan.nextLine()之后,用它来做任何事情以减少休息日。此外,我不知道scan是什么,因为您将扫描仪命名为birthdayin,然后您不使用它。

最快的变化(假设您解决scan vs birthdayin事情)将检查if ((bmonth.startsWith("01/") || bmonth.startsWith("1/"))之类的内容。

答案 1 :(得分:0)

为什么不分割日期并执行以下操作:

           Scanner birthdayin = new Scanner(System.in);
           String bmonth = birthdayin.nextLine();
           String[] parts = bmonth.split("/");
           String horoscope = null;

           int M = Integer.parseInt(parts[0]);
            int D = Integer.parseInt(parts[1]);
            if      ((M == 12 && D >= 22 && D <= 31) || (M ==  1 && D >= 1 && D <= 19))
                horoscope ="Capricorn";
            else if ((M ==  1 && D >= 20 && D <= 31) || (M ==  2 && D >= 1 && D <= 17))
                horoscope ="Aquarius";
            else if ((M ==  2 && D >= 18 && D <= 29) || (M ==  3 && D >= 1 && D <= 19))
                horoscope ="Pisces";
            else if ((M ==  3 && D >= 20 && D <= 31) || (M ==  4 && D >= 1 && D <= 19))
                horoscope ="Aries";
            else if ((M ==  4 && D >= 20 && D <= 30) || (M ==  5 && D >= 1 && D <= 20))
                horoscope ="Taurus";
            else if ((M ==  5 && D >= 21 && D <= 31) || (M ==  6 && D >= 1 && D <= 20))
                horoscope ="Gemini";
            else if ((M ==  6 && D >= 21 && D <= 30) || (M ==  7 && D >= 1 && D <= 22))
                horoscope ="Cancer";
            else if ((M ==  7 && D >= 23 && D <= 31) || (M ==  8 && D >= 1 && D <= 22))
                horoscope ="Leo";
            else if ((M ==  8 && D >= 23 && D <= 31) || (M ==  9 && D >= 1 && D <= 22))
                horoscope ="Virgo";
            else if ((M ==  9 && D >= 23 && D <= 30) || (M == 10 && D >= 1 && D <= 22))
                horoscope ="Libra";
            else if ((M == 10 && D >= 23 && D <= 31) || (M == 11 && D >= 1 && D <= 21))
                horoscope ="Scorpio";
            else if ((M == 11 && D >= 22 && D <= 30) || (M == 12 && D >= 1 && D <= 21))
                horoscope ="Sagittarius";
            else
                horoscope ="Illegal date";

            System.out.println("Your sign is: " + horoscope);