我总是得到 NULL 作为 switch 的答案

I always get NULL as a answer in switch

import javax.swing.JOptionPane;

public class Zodiac { 
        public static void main(String[] args) {



            int M = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Month"));
            int D = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Day"));
            int Y = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter year: (1900 - 2019): "));

            System.out.println("Date:"+M+ "/" +D+ "/" +Y);
            System.out.println("Month: "+M);
            System.out.println("Day: "+D);
            System.out.println("Year: "+Y);

            System.out.print("Your Zodiac Sign is: ");
            String animal = null ;

            if      ((M == 12 && D >= 22 && D <= 31) || (M ==  1 && D >= 1 && D <= 19))
                System.out.println("Capricorn");
            else if ((M ==  1 && D >= 20 && D <= 31) || (M ==  2 && D >= 1 && D <= 17))
                System.out.println("Aquarius");
            else if ((M ==  2 && D >= 18 && D <= 29) || (M ==  3 && D >= 1 && D <= 19))
                System.out.println("Pisces");
            else if ((M ==  3 && D >= 20 && D <= 31) || (M ==  4 && D >= 1 && D <= 19))
                System.out.println("Aries");
            else if ((M ==  4 && D >= 20 && D <= 30) || (M ==  5 && D >= 1 && D <= 20))
                System.out.println("Taurus");
            else if ((M ==  5 && D >= 21 && D <= 31) || (M ==  6 && D >= 1 && D <= 20))
                System.out.println("Gemini");
            else if ((M ==  6 && D >= 21 && D <= 30) || (M ==  7 && D >= 1 && D <= 22))
                System.out.println("Cancer");
            else if ((M ==  7 && D >= 23 && D <= 31) || (M ==  8 && D >= 1 && D <= 22))
                System.out.println("Leo");
            else if ((M ==  8 && D >= 23 && D <= 31) || (M ==  9 && D >= 1 && D <= 22))
                System.out.println("Virgo");
            else if ((M ==  9 && D >= 23 && D <= 30) || (M == 10 && D >= 1 && D <= 22))
                System.out.println("Libra");
            else if ((M == 10 && D >= 23 && D <= 31) || (M == 11 && D >= 1 && D <= 21))
                System.out.println("Scorpio");
            else if ((M == 11 && D >= 22 && D <= 30) || (M == 12 && D >= 1 && D <= 21))
                System.out.println("Sagittarius");
            else
                System.out.println("Invalid Date");

            {       

           switch (Y)

           {


           case 1:
           if ((Y == 1900)||( Y == 1912)||(Y == 1924 )||( Y == 1936 )||( Y ==1948 )
                ||( Y == 1960 )||( Y == 1972)||( Y == 1984)||( Y == 1996)||( Y == 2008 ))
               animal  = "Rat";
           case 2:
               if (Y == 1901||( Y ==1913 )||( Y ==1925 )||( Y ==1937 )||( Y ==1949 )
               ||( Y ==1961 )||( Y ==1973 )||( Y ==1985 )||( Y ==1997 )||( Y ==2009 ))
                   animal  = "Ox";
           case 3:
               if ((Y == 1902)||( Y ==1914 )||( Y ==1926 )||( Y ==1938 )||( Y ==1950 )
                ||( Y ==1962 )||( Y ==1973 )||( Y ==1985 )||(Y ==1998 )||( Y ==2010))
                   animal  = "Tiger";
           case 4:
               if ((Y == 1903)||( Y == 1915)||( Y== 1927)||( Y == 1939)||( Y == 1951)
                  ||( Y == 1963)||( Y == 1975)||( Y == 1987)||( Y == 1999)||( Y == 2011))
                   animal  = "Rabbit";
           case 5:
               if ((Y == 1904)||( Y == 1916)||( Y == 1928)||( Y == 1940)||( Y == 1952)
                       ||( Y == 1964)||( Y == 1976)||(Y== 1988)||( Y == 2000)||( Y == 2012))
                   animal  = "Dragon";
           case 6:
               if ((Y == 1905)||( Y== 1917)||( Y== 1929)||( Y == 1941)||( Y == 1953)||( Y == 1965)
                       ||( Y == 1977)||( Y == 1989)||( Y == 2001)||( Y == 2013))
                   animal  = "Snake";
           case 7:
               if ((Y == 1906)||( Y == 1918)||( Y == 1930)||( Y == 1942)||( Y == 1954)
                       ||( Y == 1966)||( Y == 1978)||( Y == 1990)||( Y == 2002)||( Y == 2014))
                   animal  = "Horse";
           case 8:  
               if ((Y == 1907)||( Y == 1919)||( Y == 1931)||( Y == 1943)||( Y == 1955)
                       ||( Y == 1967||( Y == 1979)||( Y == 1991)||( Y == 2003)||( Y == 2015)))             
                   animal  = "Sheep";
           case 9:
               if ((Y == 1908)||( Y == 1920)||( Y == 1932)||( Y == 1944)||( Y == 1956)
                       ||( Y == 1968)||( Y == 1980)||( Y == 1992)||( Y == 2004)||( Y == 2016))
                   animal  = "Monkey";
           case 10:
               if ((Y == 1909)||( Y == 1921)||( Y == 1933)||( Y == 1945)||( Y == 1957)
                       ||( Y == 1969)||( Y == 1981)||( Y == 1993)||( Y == 2005)||( Y == 2017))
                   animal  = "Rooster";
           case 11:
               if ((Y == 1910)||( Y == 1922)||( Y == 1934)||( Y == 1946)||( Y == 1958)
                       ||( Y == 1970)||( Y == 1982)||( Y == 1994)||( Y == 2006)||( Y == 2018))
                   animal  = "Dog";
           case 12:
               if ((Y == 1911)||( Y == 1923)||( Y == 1935)||( Y == 1947)||( Y == 1959)
                       ||( Y == 1971)||( Y == 1983)||( Y == 1995)||( Y == 2007)||( Y == 2019))
                   animal  = "Pig";

           break;

           }
           System.out.println("Your Chinese Calendar Animal: "+ animal);
        }

    }

}

示例:

Date:12/12/1948
Month: 12
Day: 12
Year: 1948
Your Zodiac Sign is: Sagittarius
Your Chinese Calendar Animal: null

您的 switch case 需要 1 到 12 的 Y 值。因此 1948 不会匹配任何 case。

除此之外,每个 case 都应以 break 结尾(即使这不会改变您示例中的输出)。

您可以通过消除 if 语句来简化代码:

       switch (Y%12)
       {
       case 4:
           animal  = "Rat";break;
       case 5:
               animal  = "Ox";break;
       case 6:
               animal  = "Tiger";break;
       case 7:
               animal  = "Rabbit";break;
       case 8:
               animal  = "Dragon";break;
       case 9:
               animal  = "Snake";break;
       case 10:
               animal  = "Horse";break;
       case 11:              
               animal  = "Sheep";break;
       case 0:
               animal  = "Monkey";break;
       case 1:
               animal  = "Rooster";break;
       case 2:
               animal  = "Dog";break;
       case 3:
               animal  = "Pig";break;
       }
       System.out.println("Your Chinese Calendar Animal: "+ animal);
    }

或者你可以把动物放在一个数组中并去掉 switch 语句:

String[] signs = {"Monkey","Rooster","Dog","Pig","Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Sheep"};
animal = signs[Y%12];

最简单的修复,去掉大小写,并使列表全部 if else 语句

if ((Y == 1900)||( Y == 1912)||(Y == 1924 )||( Y == 1936 )||( Y ==1948 )
                ||( Y == 1960 )||( Y == 1972)||( Y == 1984)||( Y == 1996)||( Y == 2008 )){
               animal  = "Rat";

}else  if (Y == 1901||( Y ==1913 )||( Y ==1925 )||( Y ==1937 )||( Y ==1949 )
               ||( Y ==1961 )||( Y ==1973 )||( Y ==1985 )||( Y ==1997 )||( Y ==2009 )){
                   animal  = "Ox";
}else if …

可能有一个更优雅的数学公式可以计算出动物的参考值,然后使用案例陈述。

不是数学上的优雅,但没有混乱的 ifs 就可以完成工作!

 while(y >1911){
    y = y-12;
 } 
 switch(y){
    case 1900 : animal = "Rat"; break;
    //etc…
    case default : "U wot m8"; break;
 }

这段代码完全不可读。这就是你迷失其中的原因。

我相信删除所有冗余将帮助您在将来非常快速地捕获此类错误。投资于此。

我的建议:

  1. 在 if/else 语句中始终使用 {} 大括号以减少歧义代码!
  2. 使用函数提取相似逻辑。
  3. 尽可能简化你的逻辑
  4. 在 switch-case 语句中使用 break

申请后您拥有:

// You need either switch or if/else here - not both
int mod = Y % 12;
switch (mod) {
case 8:
    animal = "Rat";
    break;
case 9:
    animal  = "Ox";
    break;
// and so on