Skip to content

Instantly share code, notes, and snippets.

@youngvctr
Last active June 2, 2023 19:16
Show Gist options
  • Save youngvctr/b84cb6ea0df0c6514178e1190c8ee566 to your computer and use it in GitHub Desktop.
Save youngvctr/b84cb6ea0df0c6514178e1190c8ee566 to your computer and use it in GitHub Desktop.
/*
김정훈
2023-06-01 | 입력 값을 여러번 받도록 while문을 사용하였고, 입력된 값을 정규식으로 필터링을 할 수 있도록 예외 처리를 추가하였습니다.
*/
import java.util.Scanner;
public class JavaStudy02 {
public static void main(String[] args) {
while(true)
{
boolean isChecker = false;
System.out.println("[캐시백 계산]");
System.out.printf("결제 금액을 입력해 주세요.(금액):");
String inputPrice = "";
while(!isChecker)
{
Scanner sc = new Scanner(System.in);
inputPrice = sc.next();
boolean isPrice = inputPrice.matches("[0-9]+");
if (isPrice) {
isChecker = true;
} else {
System.out.println("결제 금액에 숫자가 아닌 문자가 있습니다. 결제 금액을 다음과 같이 다시 입력해주세요. 예) 12000, 1200");
System.out.printf("결제 금액을 입력해 주세요.(금액):");
}
}
double calCashback = (int)(Integer.parseInt(inputPrice) * 0.1);
int cashback = 0;
if (calCashback >= 300) cashback = 300;
else if (calCashback >= 200) cashback = 200;
else if (calCashback >= 100) cashback = 100;
System.out.printf("결제 금액은 %s원이고, 캐시백은 %d원 입니다.\n", inputPrice, cashback);
}
}
}
@youngvctr
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment