Home > Java > javaTutorial > body text

Java Example - enum and switch statement usage

黄舟
Release: 2017-02-16 10:26:28
Original
1475 people have browsed it

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;
      }
   }
}
Copy after login

The output result of running the above code is:

你选择了 tata!
Copy after login

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)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!