Read number from console and use switch to check value - Java Language Basics

Java examples for Language Basics:switch

Description

Read number from console and use switch to check value

Demo Code

import java.util.Scanner;

public class Main
{

  static Scanner sc = new Scanner(System.in);

  public static void main(String[] args)
  {/*from w  w w  . j  a v  a2s.c o  m*/

    System.out.print("Enter the error code: ");
    int err = sc.nextInt();

    String msg;

    switch (err)
    {
      case 1:
        msg = "1.";
        break;
      case 2:
        msg = "2.";
        break;
      case 3:
        msg = "3.";
        break;
      case 4:
        msg = "4.";
        break;
      case 5:
        msg = "5.";
        break;
      case 6:
        msg = "6.";
        break;
      case 7:
        msg = "7.";
        break;
      default:
        msg = "8.";
        break;
    }
    System.out.println(msg);
  }
}

Related Tutorials