Use a switch statement for the conversion - Java Language Basics

Java examples for Language Basics:switch

Description

Use a switch statement for the conversion

Demo Code

public class Main {
  public static void main(String[] arguments) {
    long num = 0;
    char firstChar = 'a';
    char secondChar = 'i';
    switch (firstChar) {
    case 'o':
      num = 1L;/*from   w  w w .j av a  2 s.  co  m*/
      break;
    case 't':
      if (secondChar == 'w')
        num = 2L;
      if (secondChar == 'h')
        num = 3L;
      if (secondChar == 'e')
        num = 10L;
      break;
    case 'f':
      if (secondChar == 'o')
        num = 4L;
      if (secondChar == 'i')
        num = 5L;
      break;
    case 's':
      if (secondChar == 'i')
        num = 6L;
      if (secondChar == 'e')
        num = 7L;
      break;
    case 'e':
      if (secondChar == 'i')
        num = 8L;
      break;
    case 'n':
      num = 9L;
    }
    System.out.println("The number is " + num);
  }
}

Result


Related Tutorials