스터디사이트 : https://www.hackerrank.com
JAVA Basic : 연산자 (Operators)
총 식사비 구하기
※ 변수와 타입
식사비 : 세금과 팁 계산하기 전 원가를 입력받음
mealCost, Double
팁 퍼센트 : 식사비의 몇프로를 팁으로 할 건지 입력받음
tipPercent, int
세금 퍼센트 : 식사비의 몇프로를 세금으로 할 건지 입력받음
taxPercent, int
※ 계산 예
팁 : mealCost x tipPercent
세금 : mealCost x taxPercent
총 식사비 : mealCost + 팁 + 세금
소수점 버린 총 식사비 : round(총 식사비)
입력
12.00
20
8
출력
The total meal cost is 15 dollars.
코딩연습 - 1
100을 넘지 않는 int 값을 100으로 나누려고 하니 0 이 나올 수 밖에 없었다..
tipPercent와 taxPercent 값을 백분율로 어떻게든 고쳐야 할텐데...
public static void main(String[] args) { Scanner scan = new Scanner(System.in);
double mealCost = scan.nextDouble(); // original meal price int tipPercent = scan.nextInt(); // tip percentage int taxPercent = scan.nextInt(); // tax percentage scan.close();
// Write your calculation code here. double tip = tipPercent/100; System.out.println(tip); double tax = taxPercent/100; System.out.println(tax); double totalmealCost = mealCost + tip + tax; System.out.println(totalmealCost);
// cast the result of the rounding operation to an int and save it as totalCost int totalCost = (int) Math.round(totalmealCost);
// Print your result System.out.println("The total meal cost is " + totalCost + " dollars."); }
|
결과 : 0.0 0.0 12.0 The total meal cost is 12 dollars.
|
코딩연습 - 2
tipPercent와 taxPercent 값이 현재 int형인데, 이것들을 double형으로 형변환을 해주면 되는 간단한 문제였...다;;
public static void main(String[] args) { Scanner scan = new Scanner(System.in);
double mealCost = scan.nextDouble(); // original meal price int tipPercent = scan.nextInt(); // tip percentage int taxPercent = scan.nextInt(); // tax percentage scan.close();
// Write your calculation code here. double tip = (double)tipPercent/100; System.out.println(tip); double tax = (double)taxPercent/100; System.out.println(tax); double totalmealCost = mealCost + tip + tax; System.out.println(totalmealCost);
// cast the result of the rounding operation to an int and save it as totalCost int totalCost = (int) Math.round(totalmealCost);
// Print your result System.out.println("The total meal cost is " + totalCost + " dollars."); } |
결과 : 0.2 0.08 12.28 The total meal cost is 12 dollars. |
결과
public static void main(String[] args) { Scanner scan = new Scanner(System.in);
double mealCost = scan.nextDouble(); // original meal price int tipPercent = scan.nextInt(); // tip percentage int taxPercent = scan.nextInt(); // tax percentage scan.close();
// Write your calculation code here. double tip = mealCost * (double)tipPercent/100; double tax = mealCost * (double)taxPercent/100; double totalmealCost = mealCost + tip + tax;
// cast the result of the rounding operation to an int and save it as totalCost int totalCost = (int) Math.round(totalmealCost);
// Print your result System.out.println("The total meal cost is " + totalCost + " dollars."); } |
결과 : The total meal cost is 15 dollars. |
'JAVA > JAVA Basic' 카테고리의 다른 글
JAVA Basic : 클래스 상속 , super메소드의 사용 (0) | 2016.07.12 |
---|---|
JAVA Basic : Dictionary & Map 클래스 (0) | 2016.07.07 |
JAVA Basic : 짝수/홀수 자리 글자 추출하여 나열시키기 (0) | 2016.07.07 |
JAVA Basic : 클래스와 인스턴스 (0) | 2016.07.07 |
JAVA Basic : Scanner클래스의 useDelimiter() 메소드. (0) | 2016.07.01 |