logo

Java 스위치 설명

자바 스위치 문 여러 조건에서 하나의 명령문을 실행합니다. 그것은 같다 if-else-if 사다리 진술. switch 문은 byte, short, int, long, enum 유형, String 및 Byte, Short, Int 및 Long과 같은 일부 래퍼 유형에서 작동합니다. Java 7부터 다음을 사용할 수 있습니다. 문자열 스위치 문에서.

즉, switch 문은 여러 값에 대해 변수의 동일성을 테스트합니다.

기억해야 할 점

  • 있을 수있다 하나 또는 N개의 케이스 값 스위치 표현식의 경우.
  • 케이스 값은 스위치 표현식 유형이어야 합니다. 케이스 값은 다음과 같아야 합니다. 리터럴 또는 상수 . 그것은 허용하지 않습니다 변수 .
  • 케이스 값은 다음과 같아야 합니다. 고유한 . 값이 중복되면 컴파일 타임 오류가 발생합니다.
  • Java 스위치 표현식은 다음과 같아야 합니다. byte, short, int, long(Wrapper 유형 포함), 열거형 그리고 문자열 .
  • 각 사례문은 다음을 가질 수 있습니다. break 문 이는 선택 사항입니다. 통제가 다음 단계에 도달하면 break 문 , 스위치 표현식 다음에 컨트롤을 점프합니다. break 문을 찾지 못하면 다음 Case를 실행합니다.
  • 케이스 값은 기본 라벨 이는 선택 사항입니다.

통사론:

 switch(expression){ case value1: //code to be executed; break; //optional case value2: //code to be executed; break; //optional ...... default: code to be executed if all cases are not matched; } 

Switch 문의 흐름도

: 자바에서
Java의 스위치 문의 흐름

예:

SwitchExample.java

 public class SwitchExample { public static void main(String[] args) { //Declaring a variable for switch expression int number=20; //Switch expression switch(number){ //Case statements case 10: System.out.println('10'); break; case 20: System.out.println('20'); break; case 30: System.out.println('30'); break; //Default case statement default:System.out.println('Not in 10, 20 or 30'); } } } 
지금 테스트해보세요

산출:

 20 

월 찾기 예:

SwitchMonthExample.javaHTML

 //Java Program to demonstrate the example of Switch statement //where we are printing month name for the given number public class SwitchMonthExample { public static void main(String[] args) { //Specifying month number int month=7; String monthString=''; //Switch statement switch(month){ //case statements within the switch block case 1: monthString='1 - January'; break; case 2: monthString='2 - February'; break; case 3: monthString='3 - March'; break; case 4: monthString='4 - April'; break; case 5: monthString='5 - May'; break; case 6: monthString='6 - June'; break; case 7: monthString='7 - July'; break; case 8: monthString='8 - August'; break; case 9: monthString='9 - September'; break; case 10: monthString='10 - October'; break; case 11: monthString='11 - November'; break; case 12: monthString='12 - December'; break; default:System.out.println('Invalid Month!'); } //Printing month of the given number System.out.println(monthString); } } 
지금 테스트해보세요

산출:

 7 - July 

모음이나 자음을 확인하는 프로그램:

문자가 A, E, I, O 또는 U이면 모음이고 그렇지 않으면 자음입니다. 대소문자를 구분하지 않습니다.

arraylist 메소드

SwitchVowelExample.java

 public class SwitchVowelExample { public static void main(String[] args) { char ch='O'; switch(ch) { case 'a': System.out.println('Vowel'); break; case 'e': System.out.println('Vowel'); break; case 'i': System.out.println('Vowel'); break; case 'o': System.out.println('Vowel'); break; case 'u': System.out.println('Vowel'); break; case 'A': System.out.println('Vowel'); break; case 'E': System.out.println('Vowel'); break; case 'I': System.out.println('Vowel'); break; case 'O': System.out.println('Vowel'); break; case 'U': System.out.println('Vowel'); break; default: System.out.println('Consonant'); } } } 

산출:

 Vowel 

Java Switch 문이 실패했습니다.

Java 스위치 문이 실패합니다. 이는 break 문이 없으면 첫 번째 일치 이후 모든 문을 실행한다는 의미입니다.

예:

SwitchExample2.java

 //Java Switch Example where we are omitting the //break statement public class SwitchExample2 { public static void main(String[] args) { int number=20; //switch expression with int value switch(number){ //switch cases without break statements case 10: System.out.println('10'); case 20: System.out.println('20'); case 30: System.out.println('30'); default:System.out.println('Not in 10, 20 or 30'); } } } 
지금 테스트해보세요

산출:

BFS 알고리즘
 20 30 Not in 10, 20 or 30 

문자열이 포함된 Java Switch 문

Java에서는 Java SE 7부터 스위치 표현식에 문자열을 사용할 수 있습니다. Case 문은 문자열 리터럴이어야 합니다.

예:

SwitchStringExample.java

김프 선택 취소 방법
 //Java Program to demonstrate the use of Java Switch //statement with String public class SwitchStringExample { public static void main(String[] args) { //Declaring String variable String levelString='Expert'; int level=0; //Using String in Switch expression switch(levelString){ //Using String Literal in Switch case case 'Beginner': level=1; break; case 'Intermediate': level=2; break; case 'Expert': level=3; break; default: level=0; break; } System.out.println('Your Level is: '+level); } } 
지금 테스트해보세요

산출:

 Your Level is: 3 

Java 중첩 스위치 문

Java의 다른 스위치 문 안에 스위치 문을 사용할 수 있습니다. 중첩된 스위치 문이라고 합니다.

예:

NestedSwitchExample.java

 //Java Program to demonstrate the use of Java Nested Switch public class NestedSwitchExample { public static void main(String args[]) { //C - CSE, E - ECE, M - Mechanical char branch = 'C'; int collegeYear = 4; switch( collegeYear ) { case 1: System.out.println('English, Maths, Science'); break; case 2: switch( branch ) { case 'C': System.out.println('Operating System, Java, Data Structure'); break; case 'E': System.out.println('Micro processors, Logic switching theory'); break; case 'M': System.out.println('Drawing, Manufacturing Machines'); break; } break; case 3: switch( branch ) { case 'C': System.out.println('Computer Organization, MultiMedia'); break; case 'E': System.out.println('Fundamentals of Logic Design, Microelectronics'); break; case 'M': System.out.println('Internal Combustion Engines, Mechanical Vibration'); break; } break; case 4: switch( branch ) { case 'C': System.out.println('Data Communication and Networks, MultiMedia'); break; case 'E': System.out.println('Embedded System, Image Processing'); break; case 'M': System.out.println('Production Technology, Thermal Engineering'); break; } break; } } } 
지금 테스트해보세요

산출:

preg_match
 Data Communication and Networks, MultiMedia 

Switch 문의 Java Enum

Java에서는 스위치 문에서 enum을 사용할 수 있습니다. Java enum은 상수 그룹을 나타내는 클래스입니다. (최종 변수와 같이 불변). enum 키워드를 사용하고 상수를 쉼표로 구분하여 중괄호 안에 넣습니다.

예:

JavaSwitchEnumExample.java

 //Java Program to demonstrate the use of Enum //in switch statement public class JavaSwitchEnumExample { public enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat } public static void main(String args[]) { Day[] DayNow = Day.values(); for (Day Now : DayNow) { switch (Now) { case Sun: System.out.println('Sunday'); break; case Mon: System.out.println('Monday'); break; case Tue: System.out.println('Tuesday'); break; case Wed: System.out.println('Wednesday'); break; case Thu: System.out.println('Thursday'); break; case Fri: System.out.println('Friday'); break; case Sat: System.out.println('Saturday'); break; } } } } 
지금 테스트해보세요

산출:

 Sunday Monday Twesday Wednesday Thursday Friday Saturday 

Switch 문의 Java 래퍼

Java에서는 다음 네 가지를 사용할 수 있습니다. 래퍼 클래스 : 스위치 문의 Byte, Short, Integer 및 Long.

예:

WrapperInSwitchCaseExample.java

 //Java Program to demonstrate the use of Wrapper class //in switch statement public class WrapperInSwitchCaseExample { public static void main(String args[]) { Integer age = 18; switch (age) { case (16): System.out.println('You are under 18.'); break; case (18): System.out.println('You are eligible for vote.'); break; case (65): System.out.println('You are senior citizen.'); break; default: System.out.println('Please give the valid age.'); break; } } } 
지금 테스트해보세요

산출:

 You are eligible for vote.