Home  >  Article  >  Java  >  Java Example - enum and switch statement usage

Java Example - enum and switch statement usage

黄舟
黄舟Original
2017-02-16 10:26:281437browse

Java uses the enum keyword to create an enumeration type, which implies that the created types are subclasses of the java.lang.Enum class

Sample code for traversing and switching enums:

/*
 author by w3cschool.cc
 Main.java
 */

enum Car {
   lamborghini,tata,audi,fiat,honda
}
public class Main {
   public static void main(String args[]){
      Car c;
      c = Car.tata;
      switch(c) {
         case lamborghini:
         System.out.println("你选择了 lamborghini!");
         break;
         case tata:
         System.out.println("你选择了 tata!");
         break;
         case audi:
         System.out.println("你选择了 audi!");
         break;
         case fiat:
         System.out.println("你选择了 fiat!");
         break;
         case honda:
         System.out.println("你选择了 honda!");
         break;
         default:
         System.out.println("我不知道你的车型。");
         break;
      }
   }
}

The output result of running the above code is:

你选择了 tata!

The above is the content used by the Java example-enum and switch statements. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java Example - LabelNext article:Java Example - Label