我认为default放后可以。
必须后加return语句。如,
default:return new Circle();
试试吧。

解决方案 »

  1.   

    他本来的这个有return语句去掉以后好像就没有了
      

  2.   

    有固定类型返回值的方法,必须在每个可能返回的分支都要有return ***;语句,
    否则编译的时候就会出你上面说的错误。本例被你改了以后,仅三个case有return语句,如果是这三个以外的情况就会出现没有返回值。
      

  3.   

    public static Shape randShape() {
        switch((int)(Math.random() * 3)) {
          //default:
          case 0: return new Circle();
          case 1: return new Square();
          case 2: return new Triangle();
        }
      }
    这个函数不能保证总有返回值啊,因为如果除了上面的三种情况以外就没有返回值了,
    所以会编译失败。
    可以改为:
      public static Shape randShape() {
        switch((int)(Math.random() * 3)) {
          //default:
          case 0: return new Circle();
          case 1: return new Square();
          case 2: return new Triangle();
        }
        return null;
      }
    或者加上default:return null;
    -------------------------------
    楼上和搂主是一个人吧?怎么不使用同一个账号,难道是为了倒分,如果是这样可不道德啊。
      

  4.   

    To rocky_chencn() ,   airhork(张逍) ,     geyf(我在学java),   wanglh2000   可是原来的程序在default后面也没有return语句呀,可是编译是可以通过的,
    只不过default必须放在最前面,放在后面则不行,我主要想知道放在前面和
    放在后面为什么会有不同的结果?另外(int)(Math.random() * 3)的值只会有三种可能,即:0,1,2.这样应该不会
    有其它的情况出现,那我们还需要default语句吗?补充一下,我和geyf(我在学java)不是同一个人,所以不会有倒分这一说。当然
    我肯定会给大家分的。我现在穷的就剩下分了。我的可用分有10055呢。
      

  5.   

    defaut:后面是不是没有语句了?
    说明default语句没结束
    default:
    case 0: return new Circle();
    这是一个完整的句子,也就是说,switch值不管等于什么值,都要向下执行你可以把default放在
    case 0: return new Circle();
          case 1: return new Square();
          case 2: return new Triangle();
    后面,
    default: return null;
    我想这样应该就可以了把return null;放在default:后面就可以了
      

  6.   

    UP无极限
    不兼容又到此混分 ;)
        ("`-''-/").___..--''"`-._ 
         `6_ 6  )   `-.  (     ).`-.__.`) 
         (_Y_.)'  ._   )  `._ `. ``-..-' 
       _..`--'_..-_/  /--'_.' ,' 
      (il),-''  (li),'  ((!.-' 
     
      不兼容 的形象大使,欢迎盗版!
      

  7.   

    搂主:
    把switch、case再好好看看,case后面只要没有return或者break之类的
    跳转语句,都是一句一句顺序执行的。to:wanglh2000() ( ) 
    不懂你的意思,我跟搂主象吗?
      

  8.   

    不好意思啊。
    虽然(int)(Math.random() * 3)的值只会有三种可能,即:0,1,2.
    但是在编译的时候编译工具还是会认为有其他情况的。
    关于switch的用法我找了点文档,你看看吧。switch 14.9 The switch Statement The switch statement transfers control to one of several statements depending on the value of an expression. SwitchStatement:  switch ( Expression ) SwitchBlockSwitchBlock:  { SwitchBlockStatementGroupsopt SwitchLabelsopt }SwitchBlockStatementGroups:  SwitchBlockStatementGroup  SwitchBlockStatementGroups SwitchBlockStatementGroupSwitchBlockStatementGroup:  SwitchLabels BlockStatementsSwitchLabels:  SwitchLabel  SwitchLabels SwitchLabelSwitchLabel:  case ConstantExpression :  default : The type of the Expression must be char, byte, short, or int, or a compile-time error occurs. The body of a switch statement must be a block. Any statement immediately contained by the block may be labeled with one or more case or default labels. These labels are said to be associated with the switch statement, as are the values of the constant expressions (§15.27) in the case labels. All of the following must be true, or a compile-time error will result: Every case constant expression associated with a switch statement must be assignable (§5.2) to the type of the switch Expression. No two of the case constant expressions associated with a switch statement may have the same value. At most one default label may be associated with the same switch statement. In C and C++ the body of a switch statement can be a statement and statements with case labels do not have to be immediately contained by that statement. Consider the simple loop: for (i = 0; i < n; ++i) foo();
     where n is known to be positive. A trick known as Duff's device can be used in C or C++ to unroll the loop, but this is not valid Java code: 
    int q = (n+7)/8;
    switch (n%8) {
    case 0:      do {    foo();        // Great C hack, Tom,
    case 7:          foo();        // but it's not valid in Java.
    case 6:          foo();
    case 5:          foo();
    case 4:          foo();
    case 3:          foo();
    case 2:          foo();
    case 1:          foo();
         } while (--q >= 0);
    }
     Fortunately, this trick does not seem to be widely known or used. Moreover, it is less needed nowadays; this sort of code transformation is properly in the province of state-of-the-art optimizing compilers. When the switch statement is executed, first the Expression is evaluated. If evaluation of the Expression completes abruptly for some reason, the switch statement completes abruptly for the same reason. Otherwise, execution continues by comparing the value of the Expression with each case constant. Then there is a choice: If one of the case constants is equal to the value of the expression, then we say that the case matches, and all statements after the matching case label in the switch block, if any, are executed in sequence. If all these statements complete normally, or if there are no statements after the matching case label, then the entire switch statement completes normally. If no case matches but there is a default label, then all statements after the matching default label in the switch block, if any, are executed in sequence. If all these statements complete normally, or if there are no statements after the default label, then the entire switch statement completes normally. If no case matches and there is no default label, then no further action is taken and the switch statement completes normally. If any statement immediately contained by the Block body of the switch statement completes abruptly, it is handled as follows: If execution of the Statement completes abruptly because of a break with no label, no further action is taken and the switch statement completes normally. If execution of the Statement completes abruptly for any other reason, the switch statement completes abruptly for the same reason. The case of abrupt completion because of a break with a label is handled by the general rule for labeled statements (§14.6). As in C and C++, execution of statements in a switch block "falls through labels" in Java. For example, the program: 
    class Toomany {  static void howMany(int k) {
       switch (k) {
       case 1:     System.out.print("one ");
       case 2:     System.out.print("too ");
       case 3:     System.out.println("many");
       }
     }  public static void main(String[] args) {
       howMany(3);
       howMany(2);
       howMany(1);
     }
    }
     contains a switch block in which the code for each case falls through into the code for the next case. As a result, the program prints: 
    many
    too many
    one too many
     If code is not to fall through case to case in this manner, then break statements should be used, as in this example: 
    class Twomany {  static void howMany(int k) {
       switch (k) {
       case 1:     System.out.println("one");
             break;          // exit the switch
       case 2:     System.out.println("two");
             break;          // exit the switch
       case 3:     System.out.println("many");
             break;          // not needed, but good style
       }
     }  public static void main(String[] args) {
       howMany(1);
       howMany(2);
       howMany(3);
     }
    }
     This program prints: 
    one
    two
    many
      

  9.   

    case The case keyword defines a group of statements to begin executing if a value specified matches the value defined by a preceding switch keyword. 14.9 The switch Statement The switch statement transfers control to one of several statements depending on the value of an expression. SwitchStatement:  switch ( Expression ) SwitchBlockSwitchBlock:  { SwitchBlockStatementGroupsopt SwitchLabelsopt }SwitchBlockStatementGroups:  SwitchBlockStatementGroup  SwitchBlockStatementGroups SwitchBlockStatementGroupSwitchBlockStatementGroup:  SwitchLabels BlockStatementsSwitchLabels:  SwitchLabel  SwitchLabels SwitchLabelSwitchLabel:  case ConstantExpression :  default : The type of the Expression must be char, byte, short, or int, or a compile-time error occurs. The body of a switch statement must be a block. Any statement immediately contained by the block may be labeled with one or more case or default labels. These labels are said to be associated with the switch statement, as are the values of the constant expressions in the case labels. All of the following must be true, or a compile-time error will result: Every case constant expression associated with a switch statement must be assignable to the type of the switch Expression. No two of the case constant expressions associated with a switch statement may have the same value. At most one default label may be associated with the same switch statement. In C and C++ the body of a switch statement can be a statement and statements with case labels do not have to be immediately contained by that statement. Consider the simple loop: for (i = 0; i < n; ++i) foo();
     where n is known to be positive. A trick known as Duff's device can be used in C or C++ to unroll the loop, but this is not valid Java code: 
    int q = (n+7)/8;
    switch (n%8) {
    case 0:      do {    foo();        // Great C hack, Tom,
    case 7:          foo();        // but it's not valid in Java.
    case 6:          foo();
    case 5:          foo();
    case 4:          foo();
    case 3:          foo();
    case 2:          foo();
    case 1:          foo();
         } while (--q >= 0);
    }
     Fortunately, this trick does not seem to be widely known or used. Moreover, it is less needed nowadays; this sort of code transformation is properly in the province of state-of-the-art optimizing compilers. When the switch statement is executed, first the Expression is evaluated. If evaluation of the Expression completes abruptly for some reason, the switch statement completes abruptly for the same reason. Otherwise, execution continues by comparing the value of the Expression with each case constant. Then there is a choice: If one of the case constants is equal to the value of the expression, then we say that the case matches, and all statements after the matching case label in the switch block, if any, are executed in sequence. If all these statements complete normally, or if there are no statements after the matching case label, then the entire switch statement completes normally. If no case matches but there is a default label, then all statements after the matching default label in the switch block, if any, are executed in sequence. If all these statements complete normally, or if there are no statements after the default label, then the entire switch statement completes normally. If no case matches and there is no default label, then no further action is taken and the switch statement completes normally. If any statement immediately contained by the Block body of the switch statement completes abruptly, it is handled as follows: If execution of the Statement completes abruptly because of a break with no label, no further action is taken and the switch statement completes normally. If execution of the Statement completes abruptly for any other reason, the switch statement completes abruptly for the same reason. The case of abrupt completion because of a break with a label is handled by the general rule for labeled statements. As in C and C++, execution of statements in a switch block "falls through labels" in Java. For example, the program: 
    class Toomany {  static void howMany(int k) {
       switch (k) {
       case 1:     System.out.print("one ");
       case 2:     System.out.print("too ");
       case 3:     System.out.println("many");
       }
     }  public static void main(String[] args) {
       howMany(3);
       howMany(2);
       howMany(1);
     }
    }
     contains a switch block in which the code for each case falls through into the code for the next case. As a result, the program prints: 
    many
    too many
    one too many
     If code is not to fall through case to case in this manner, then break statements should be used, as in this example: 
    class Twomany {  static void howMany(int k) {
       switch (k) {
       case 1:     System.out.println("one");
             break;          // exit the switch
       case 2:     System.out.println("two");
             break;          // exit the switch
       case 3:     System.out.println("many");
             break;          // not needed, but good style
       }
     }  public static void main(String[] args) {
       howMany(1);
       howMany(2);
       howMany(3);
     }
    }
     This program prints: 
    one
    two
    many
      

  10.   

    default The default keyword can be used after all case conditions in a switch statement. If all case conditions are not matched by the value of the switch variable, the default keyword will be executed. 14.9 The switch Statement The switch statement transfers control to one of several statements depending on the value of an expression. SwitchStatement:  switch ( Expression ) SwitchBlockSwitchBlock:  { SwitchBlockStatementGroupsopt SwitchLabelsopt }SwitchBlockStatementGroups:  SwitchBlockStatementGroup  SwitchBlockStatementGroups SwitchBlockStatementGroupSwitchBlockStatementGroup:  SwitchLabels BlockStatementsSwitchLabels:  SwitchLabel  SwitchLabels SwitchLabelSwitchLabel:  case ConstantExpression :  default : The type of the Expression must be char, byte, short, or int, or a compile-time error occurs. The body of a switch statement must be a block. Any statement immediately contained by the block may be labeled with one or more case or default labels. These labels are said to be associated with the switch statement, as are the values of the constant expressions in the case labels. All of the following must be true, or a compile-time error will result: Every case constant expression associated with a switch statement must be assignable to the type of the switch Expression. No two of the case constant expressions associated with a switch statement may have the same value. At most one default label may be associated with the same switch statement. In C and C++ the body of a switch statement can be a statement and statements with case labels do not have to be immediately contained by that statement. Consider the simple loop: for (i = 0; i < n; ++i) foo();
     where n is known to be positive. A trick known as Duff's device can be used in C or C++ to unroll the loop, but this is not valid Java code: 
    int q = (n+7)/8;
    switch (n%8) {
    case 0:      do {    foo();        // Great C hack, Tom,
    case 7:          foo();        // but it's not valid in Java.
    case 6:          foo();
    case 5:          foo();
    case 4:          foo();
    case 3:          foo();
    case 2:          foo();
    case 1:          foo();
         } while (--q >= 0);
    }
     Fortunately, this trick does not seem to be widely known or used. Moreover, it is less needed nowadays; this sort of code transformation is properly in the province of state-of-the-art optimizing compilers. When the switch statement is executed, first the Expression is evaluated. If evaluation of the Expression completes abruptly for some reason, the switch statement completes abruptly for the same reason. Otherwise, execution continues by comparing the value of the Expression with each case constant. Then there is a choice: If one of the case constants is equal to the value of the expression, then we say that the case matches, and all statements after the matching case label in the switch block, if any, are executed in sequence. If all these statements complete normally, or if there are no statements after the matching case label, then the entire switch statement completes normally. If no case matches but there is a default label, then all statements after the matching default label in the switch block, if any, are executed in sequence. If all these statements complete normally, or if there are no statements after the default label, then the entire switch statement completes normally. If no case matches and there is no default label, then no further action is taken and the switch statement completes normally. If any statement immediately contained by the Block body of the switch statement completes abruptly, it is handled as follows: If execution of the Statement completes abruptly because of a break with no label, no further action is taken and the switch statement completes normally. If execution of the Statement completes abruptly for any other reason, the switch statement completes abruptly for the same reason. The case of abrupt completion because of a break with a label is handled by the general rule for labeled statements. As in C and C++, execution of statements in a switch block "falls through labels" in Java. For example, the program: 
    class Toomany {  static void howMany(int k) {
       switch (k) {
       case 1:     System.out.print("one ");
       case 2:     System.out.print("too ");
       case 3:     System.out.println("many");
       }
     }  public static void main(String[] args) {
       howMany(3);
       howMany(2);
       howMany(1);
     }
    }
     contains a switch block in which the code for each case falls through into the code for the next case. As a result, the program prints: 
    many
    too many
    one too many
     If code is not to fall through case to case in this manner, then break statements should be used, as in this example: 
    class Twomany {  static void howMany(int k) {
       switch (k) {
       case 1:     System.out.println("one");
             break;          // exit the switch
       case 2:     System.out.println("two");
             break;          // exit the switch
       case 3:     System.out.println("many");
             break;          // not needed, but good style
       }
     }  public static void main(String[] args) {
       howMany(1);
       howMany(2);
       howMany(3);
     }
    }
     This program prints: 
    one
    two
    many
      

  11.   

    这个问题我在csdn和cjsdn分别提问,大家可以参照这两个帖子。
    csdn:http://expert.csdn.net/Expert/topic/1877/1877411.xml?temp=.4656793
    cjsdn:http://www.cjsdn.com/post/view?bid=1&id=31157&sty=1
    非常感谢大家这么耐心而又细致的解答,谢谢!综合大家的观点和我自已的理解,做一总结如下:
    1.为什么需要 default 语句?在randShape()中,其实 (int)(Math.random() * 3 的结果只有三种可能,就是:0,1,2
    好象default语句是多余的,实际上(int)(Math.random() * 3 的值只有在运行期才会得出,
    而编译器事先无法知道只可能有三种结果,它认为还有其它的结果,并且 randShape() 要求
    必须返回一个 Shape object ,因此编译器需要一个default语句。其实在运行期,永远都不会执行 default 后面的子句,因为不可能出现三种结果以外的
    情况,加入default 语句只是为了让编译器顺利通过编译。这也是最初让我困惑的地方,在我的头脑中已经有了(int)(Math.random() * 3 只有三种
    结果的想法,而这一切编译器不知道,我的思维方式与编译器的运作方式出现了错位。eet 老兄的示例很好,让我对这一问题有了了解。详见 eet 的帖子。
    2.为什么 default 语句(在其后没有子句的情况下)不能放在switch语句块的块尾。  public static Shape randShape() {
        switch((int)(Math.random() * 3)) {
          default:
          case 0: return new Circle();
          case 1: return new Square();
          case 2: return new Triangle();
        }
      }  等价于  public static Shape randShape() {
        switch((int)(Math.random() * 3)) {
          default: return new Circle();
          case 0: return new Circle();
          case 1: return new Square();
          case 2: return new Triangle();
        }
      }  上面是default放在块头的情形。default 放在其它地方也可以,只是不能放在块尾
      (在default后没有子句的情况下),当执行到default语句时,由于其后并无break,
      return语句,它会执行紧接于其后的语句(尽管在运行期default语句不会被执行)。  public static Shape randShape() {
        switch((int)(Math.random() * 3)) {
          case 0: return new Circle();
          case 1: return new Square();
          case 2: return new Triangle();
          default:
        }
      }  编译器认为在三种结果之外情况下, randShape() 不能获得返回值。所以它不允许
      编译通过。
      有好几位朋友都提到这一点。
      到此这个问题有了最终的解答,如果没有人对我以上的总结提出异议或补充的话,明天就
      可以结帖了。再次感谢大家让我这么快就获得了答案。