3일 차 JAVA switch문과 if문의 활용 구분하기
switch문과 if문의 차이점을 알아보기 위해
간단한 코드를 작성해 보았다.
switch문
소스코드
-------------------------
import java.util.Scanner;
public class Test11 {
public static void main(String arg[]) {
int score;
int random_value;
Scanner scan = new Scanner(System.in);
System.out.println("1~10사이의 점수를 입력하세요");
score = scan.nextInt();
random_value = (int) (Math.random() * score);
switch (random_value) {
case 1:
System.out.println("Bananas");
break;
case 2:
System.out.println("Oranges");
break;
case 3:
System.out.println("Pears");
case 4:
System.out.println("Apples");
case 5:
System.out.println("Plums");
break;
case 6:
System.out.println("Pineapples");
break;
case 7:
break;
default:
System.out.println("Nuts");
break;
}
}
}
if문
소스코드
-------------------------
import java.util.Scanner;
import javax.print.attribute.standard.Finishings;
public class Test12 {
public static void main(String arg[]) {
int score;
int random_value;
Scanner scan = new Scanner(System.in);
System.out.println("1~10사이의 점수를 입력하세요");
score = scan.nextInt();
random_value = (int) (Math.random() * score);
if (random_value == 1) {
System.out.println("Bananas");
} else if (random_value == 2) {
System.out.println("Oranges");
} else if (random_value == 3) {
System.out.println("Pears");
System.out.println("Apples");
System.out.println("Plums");
} else if (random_value == 4) {
System.out.println("Pears");
System.out.println("Apples");
} else if (random_value == 5) {
System.out.println("Pears");
} else if (random_value == 6) {
System.out.println("Pineapples");
} else if (random_value == 7) {
} else {
System.out.println("Nuts");
}
}
}
출처: 삼성SDS멀티캠퍼스
강사: 홍승길
Email : iccack70@gmail.com