【问题标题】:I always get NULL as a answer in switch我总是在 switch 中得到 NULL 作为答案
【发布时间】:2015-01-18 12:31:00
【问题描述】:
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

【问题讨论】:

  • 您需要在每种情况下添加break 语句,否则它将“失败”:docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
  • 你应该重新考虑你的设计。你有所有这些 &amp;&amp;|| 的错误工厂。
  • 金,你不能同时接受这两个答案,所以你必须选择一个你喜欢接受的答案。

标签: java swing switch-statement


【解决方案1】:

您的 switch case 期望 Y 值为 1 到 12。因此 1948 不会匹配任何大小写。

除此之外,每个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];

【讨论】:

    【解决方案2】:

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

    我确信删除所有冗余将帮助您在未来很快发现此类错误。投资。

    我的建议:

    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
    

    【讨论】:

      【解决方案3】:

      最简单的解决方法,去掉case,把列表全部列出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 …
      

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

      不是数学上的优雅,但可以在没有混乱的 if 的情况下完成这项工作!

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-31
        • 1970-01-01
        • 2018-01-14
        • 1970-01-01
        • 2021-08-16
        • 2019-04-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多