Java - 切换功能

时间:2013-11-03 14:53:49

标签: java switch-statement

Down bellow是一个代码的一部分,用于计算单词的元音数量。第一个代码与第二个代码做同样的事情?所以,如果我想使用相同操作的几个案例,我只需要将操作放在最后一个(就像在第一个代码中一样)?

首先:

public static int contvowel(String pal){ 
        int cont=0; 
        pal=pal.toLowerCase(); 
        for(int i=0; i<pal.length();i++) { 
            switch(pal.charAt(i)){ 
                case 'a': 
                case 'e': 
                case 'i': 
                case 'o': 
                case 'u': { cont=cont+1; break; }
            } 
        } 
            return cont; 
        } 

第二

public static int contvowel(String pal){ 
        int cont=0; 
        pal=pal.toLowerCase(); 
        for(int i=0; i<pal.length();i++) { 
            switch(pal.charAt(i)){ 
                case 'a': cont=cont+1; break;
                case 'e': cont=cont+1; break;
                case 'i': cont=cont+1; break;
                case 'o': cont=cont+1; break;
                case 'u': cont=cont+1; break;
            } 
        } 
            return cont; 
        } 

提前致谢。 :)

3 个答案:

答案 0 :(得分:5)

回答你的问题,“是” - 结果是一样的。


但这里有一些值得思考的东西:

public static int countVowels(String pal) { 
    return pal.replaceAll("[^aeiou]", "").length();
}

答案 1 :(得分:1)

你的答案是:

是。第一个代码与第二个代码相同。

答案 2 :(得分:0)

结果结果将是相同的。但是对于“代码使得相同”,默许 NO

编辑:如评论中所述,这些是两个编译版本:

版本1:

Compiled from "Hello.java"
public class main.java.Hello {
  public main.java.Hello();
    Code:
       0: aload_0       
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return        

  public static void main(java.lang.String[]);
    Code:
       0: ldc           #2                  // String Hello, World!
       2: astore_1      
       3: iconst_0      
       4: istore_2      
       5: aload_1       
       6: invokevirtual #3                  // Method java/lang/String.toLowerCase:()Ljava/lang/String;
       9: astore_1      
      10: iconst_0      
      11: istore_3      
      12: iload_3       
      13: aload_1       
      14: invokevirtual #4                  // Method java/lang/String.length:()I
      17: if_icmpge     86
      20: aload_1       
      21: iload_3       
      22: invokevirtual #5                  // Method java/lang/String.charAt:(I)C
      25: lookupswitch  { // 5
                    97: 76
                   101: 76
                   105: 76
                   111: 76
                   117: 76
               default: 80
          }
      76: iload_2       
      77: iconst_1      
      78: iadd          
      79: istore_2      
      80: iinc          3, 1
      83: goto          12
      86: getstatic     #6                  // Field java/lang/System.out:Ljava/io/PrintStream;
      89: new           #7                  // class java/lang/StringBuilder
      92: dup           
      93: invokespecial #8                  // Method java/lang/StringBuilder."<init>":()V
      96: ldc           #9                  // String Got: 
      98: invokevirtual #10                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     101: iload_2       
     102: invokevirtual #11                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     105: invokevirtual #12                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     108: invokevirtual #13                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
     111: return        
}

第2版:

Compiled from "Hello.java"
public class main.java.Hello {
  public main.java.Hello();
    Code:
       0: aload_0       
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return        

  public static void main(java.lang.String[]);
    Code:
       0: ldc           #2                  // String Hello, World!
       2: astore_1      
       3: iconst_0      
       4: istore_2      
       5: aload_1       
       6: invokevirtual #3                  // Method java/lang/String.toLowerCase:()Ljava/lang/String;
       9: astore_1      
      10: iconst_0      
      11: istore_3      
      12: iload_3       
      13: aload_1       
      14: invokevirtual #4                  // Method java/lang/String.length:()I
      17: if_icmpge     114
      20: aload_1       
      21: iload_3       
      22: invokevirtual #5                  // Method java/lang/String.charAt:(I)C
      25: lookupswitch  { // 5
                    97: 76
                   101: 83
                   105: 90
                   111: 97
                   117: 104
               default: 108
          }
      76: iload_2       
      77: iconst_1      
      78: iadd          
      79: istore_2      
      80: goto          108
      83: iload_2       
      84: iconst_1      
      85: iadd          
      86: istore_2      
      87: goto          108
      90: iload_2       
      91: iconst_1      
      92: iadd          
      93: istore_2      
      94: goto          108
      97: iload_2       
      98: iconst_1      
      99: iadd          
     100: istore_2      
     101: goto          108
     104: iload_2       
     105: iconst_1      
     106: iadd          
     107: istore_2      
     108: iinc          3, 1
     111: goto          12
     114: getstatic     #6                  // Field java/lang/System.out:Ljava/io/PrintStream;
     117: new           #7                  // class java/lang/StringBuilder
     120: dup           
     121: invokespecial #8                  // Method java/lang/StringBuilder."<init>":()V
     124: ldc           #9                  // String Got: 
     126: invokevirtual #10                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     129: iload_2       
     130: invokevirtual #11                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     133: invokevirtual #12                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     136: invokevirtual #13                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
     139: return        
}

diff -e ou1 ou2

39,51c
      80: goto          108
      83: iload_2       
      84: iconst_1      
      85: iadd          
      86: istore_2      
      87: goto          108
      90: iload_2       
      91: iconst_1      
      92: iadd          
      93: istore_2      
      94: goto          108
      97: iload_2       
      98: iconst_1      
      99: iadd          
     100: istore_2      
     101: goto          108
     104: iload_2       
     105: iconst_1      
     106: iadd          
     107: istore_2      
     108: iinc          3, 1
     111: goto          12
     114: getstatic     #6                  // Field java/lang/System.out:Ljava/io/PrintStream;
     117: new           #7                  // class java/lang/StringBuilder
     120: dup           
     121: invokespecial #8                  // Method java/lang/StringBuilder."<init>":()V
     124: ldc           #9                  // String Got: 
     126: invokevirtual #10                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     129: iload_2       
     130: invokevirtual #11                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     133: invokevirtual #12                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     136: invokevirtual #13                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
     139: return        
.
29,33c
                   101: 83
                   105: 90
                   111: 97
                   117: 104
               default: 108
.
23c
      17: if_icmpge     114