Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,940 questions

51,877 answers

573 users

How to use switch statement in Java

8 Answers

0 votes
import java.util.Calendar;
 
public class Example {
    public static void main(String[] args) {
              
        int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
 
        switch (month) {
            case 1:  System.out.println("January"); break;
            case 2:  System.out.println("February"); break;
            case 3:  System.out.println("March"); break;
            case 4:  System.out.println("April"); break;
            case 5:  System.out.println("May"); break;
            case 6:  System.out.println("June"); break;
            case 7:  System.out.println("July"); break;
            case 8:  System.out.println("August"); break;
            case 9:  System.out.println("September"); break;
            case 10: System.out.println("October"); break;
            case 11: System.out.println("November"); break;
            case 12: System.out.println("December"); break;
            default: System.out.println("Error"); break;
        }
    }
}
 
 
/*
run:
  
May
  
*/

 



answered Jan 14, 2016 by avibootz
edited May 14, 2024 by avibootz
0 votes
public class Example {
    public static void main(String[] args) {
              
        char ch = 'b';
 
        switch (ch) {
                case 'a':
                    System.out.println('a');
                    break;
                case 'b':
                    System.out.println('b');
                    break;
                case 'c':
                    System.out.println('c');
                    break;
                default:
                    System.out.println("Not a, b, or c");
        }
    }
}

 
 
/*
run:
  
b
  
*/

 



answered Jan 14, 2016 by avibootz
edited May 14, 2024 by avibootz
0 votes
public class Example {
    public static void main(String[] args) {
              
        String s = "abc";
 
        switch (s) {
              case "xyz":
                    System.out.println("xyz");
                    break;
              case "mmh3z":
                    System.out.println("mmh3z");
                    break;
              case "abc":
                    System.out.println("abc");
                    break;
              default:
                    System.out.println("Not xyz, mmh3z, or abc");
        }
    }
}
 

 
/*
run:
  
abc
  
*/

 



answered Jan 14, 2016 by avibootz
edited May 14, 2024 by avibootz
0 votes
public class JavaApplication {
     public static void main(String[] args) {
         try {
             for (int i = 0; i <= 3; i++) {
                switch (i) {
                    case 1: {
                        System.out.println("One: " + i);
                        break;
                    }
                    case 2:
                    case 3: {
                        System.out.println("Two or Three: " + i);
                        break;
                    }
                    default: {
                        System.out.println("Default case: " + i);
                    }
                }
            }
 
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}
 
 
/*
              
run:
 
Default case: 0
One: 1
Two or Three: 2
Two or Three: 3
     
*/

 

 



answered Nov 28, 2016 by avibootz
edited May 14, 2024 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        int n = 3;
         
        switch (n) {
            case 0: System.out.println("0");
                   break;
            case 1: System.out.println("1");
                   break;
            case 2: System.out.println("2");
                   break;
            case 3: System.out.println("3");
                   break;
            default: System.out.println("default");           
        }
    }
}
 

 
/*
run:
 
3
 
*/

 



answered May 14, 2024 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        int n = 3;
         
        switch (n) {
            case 0: 
            case 1: 
            case 2: 
            case 3: System.out.println("0 1 2 3");
                    break;
            case 4: System.out.println("4");
                    break;
            default: System.out.println("default");           
        }
    }
}
 

 
/*
run:
 
0 1 2 3
 
*/

 



answered May 14, 2024 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        int n = 3;
         
        switch (n / 2) {
            case 0: System.out.println("0");
                    break;
            case 1: System.out.println("1");
                    break;
            case 2: System.out.println("2");
                    break;
            case 3: System.out.println("3");
                    break;
            default: System.out.println("default");           
        }
    }
}
 

 
/*
run:
 
1
 
*/

 



answered May 14, 2024 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        int n = 3;
         
        switch (n - 3) {
            case 0: System.out.println("0");
                    break;
            case 1: System.out.println("1");
                    break;
            case 2: System.out.println("2");
                    break;
            case 3: System.out.println("3");
                    break;
            default: System.out.println("default");           
        }
    }
}
 
 
/*
run:
 
0
 
*/

 



answered May 14, 2024 by avibootz

Related questions

1 answer 64 views
1 answer 125 views
1 answer 124 views
2 answers 190 views
2 answers 251 views
1 answer 183 views
...