【问题标题】:I am getting error: class, interface, or enum expected and I can't figure it out我收到错误:预期的类、接口或枚举,但我无法弄清楚
【发布时间】:2015-06-15 15:49:11
【问题描述】:

我已经为此工作了三天,但我无法解决任何问题。请帮忙!

public static void displayType (String ntype)
   {
       switch (ntype)

           case "African":
           System.out.print ("This Elephant is African")
           break;

           case "Indian":
           System.out.print ("This Elephant is Indian")
           break;

           default :
           System.out.print ("This type of Elephant is invalid")

           return ntype;
     } //End Switch


  } 

这些是我得到的错误:

Lab2Unit4Psuedocode.java:108: error: class, interface, or enum expected
 public static void displayType (String ntype)
               ^

Lab2Unit4Psuedocode.java:116: error: class, interface, or enum expected
               case "Indian":
               ^

Lab2Unit4Psuedocode.java:120: error: class, interface, or enum expected
               default :
                       ^

Lab2Unit4Psuedocode.java:124: error: class, interface, or enum expected
         } //End Switch
         ^
4 errors

【问题讨论】:

  • switch 的左大括号在哪里(因为你有右大括号)?
  • 底部的那个?那是我用来关闭整个代码的那个。我没有把整件事都放在这里,因为我只在最后一部分出现错误。我设法修复了其余部分。如果你愿意,我可以全部放上来。
  • 很可能你在这上面的某个地方有一个额外的},所以它实际上不在一个类中。
  • 你不能在void 方法中写return something;。此外,switch 语句需要一对{ } 围绕所有case 部分,包括default。因此,将{ 添加到带有switch 的行的末尾,并删除return ntype;
  • 哦,还有一件事。这段代码是否在一个类中?应该是的。你不能有一个方法只是“赤裸”地放在一个文件中。

标签: java class interface enums


【解决方案1】:

请看下面代码中的 cmets。

public class Lab2Unit4Psuedocode {

   public static void displayType (String ntype) {
       switch (ntype) { //you need the open curly brace
           case "African":
                System.out.println("This Elephant is African"); //<-- you need to terminate with semi-colons in Java
                break;

           case "Indian":
                System.out.println("This Elephant is Indian");
                break;

           default:
                System.out.println("This type of Elephant is invalid");
                //some people add an explicit break here
         //don't return anything.  By definition, void means you return nothing.
       }
   }
} //always line up your curly braces

另请注意,在 String 数据类型上使用 switch 仅在 Java 7 中出现,因此这不适用于旧版本的 Java。

Java 需要大量练习 - 坚持下去!

【讨论】:

    【解决方案2】:
    public static String displayType (String ntype)
    {
       switch (ntype) {
      // Only version 1.7 or higher can support the string literal in switch
           case "African":
           System.out.print ("This Elephant is African");
           break;
    
           case "Indian":
           System.out.print ("This Elephant is Indian"); 
           break;
    
           default :
           System.out.print ("This type of Elephant is invalid");
    
           return ntype;
        } //End Switch
    
    
     } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多