JAVA/조건식
조건식(if else-if, switch-case)예제
by pms93
2022. 7. 19.
package conditions;
import java.util.Scanner;
public class ConditionsQuiz {
public static void main(String[] args) {
int price = 0, cnt;
Scanner sc = new Scanner(System.in);
System.out.println("과자 개수 입력 : ");
cnt = sc.nextInt();
if (cnt < 10)
price = cnt * 1000;
else if (cnt <= 99)
price = (int) (cnt * 1000 * 0.9);
else
price = (int) (cnt * 1000 * 0.88);
System.out.printf("과자 %d개의 총 가격 : %d\n", cnt, price);
int time, price2 = 30000;
System.out.println("탑승시간 입력 : ");
time = sc.nextInt();
if (time > 30)
price += ((time - 21) / 10) * 5000;
System.out.println("요금 : " + price2);
String homeAdress = null, companyAdress = null;
int sel;
boolean roof = true;
while (roof) {
System.out.printf("1. 우리집 등록\n2. 회사 등록\n3. 등록 보기\n4. 종료\n: ");
sel = sc.nextInt();
sc.nextLine();
switch (sel) {
case 1:
System.out.println("집주소 입력 : ");
homeAdress = sc.nextLine();
break;
case 2:
System.out.println("회사주소 입력 : ");
companyAdress = sc.nextLine();
break;
case 3:
System.out.printf("집 주소 : %s\n회사 주소 : %s\n", homeAdress, companyAdress);
break;
case 4:
System.out.println("종료합니다.");
roof = false;
break;
default:
System.out.println("잘못된 입력입니다. 다시 입력하세요.");
}
}
}
}