② 메소드와 조건문 - 연습문제 1) 왜 에러 나는지 잘 모르겠습니다 ㅠ => printf( ) 출력시, %d %f 셋팅 잘못한 단순 실수 => int dollar = dice() + dice() + dice(); double won = exchange(dollar); System.out.printf("획득 금액: $%.2f(%d원)", dollar, won); => dollar는 int니까 %d 이어야 함 => won은 double이니까 %f 이어야 함 2) dice () + dice () + dice () 의 합은 int 값인 거 같은데 정답코드에는 double로 적혀있습니다 => double dollar = dice() + dice() + dice(); => int인데 double 적음으로써, 바로 double로 캐스팅 시켜버리기 => int -> double로의 캐스팅 OK (잃을 게 x 니까) # 돈 놓고 돈 먹기 #### CODE <a class='btn btn-default' href='/codes/21869'>Link</a> ``` public class Game { public static void main (String[] args) { int dollar = dice() + dice() + dice(); double won = exchange(dollar); System.out.printf("획득 금액: $%.2f(%d원)", dollar, won); } public static int dice(){ int result = (int)(Math.random()*6 + 1); return result; } public static double exchange(int A){ double result = A * 1082.25108; return result; } } ``` #### INPUT ``` ``` #### OUPUT ``` 획득 금액: $Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302) at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806) at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753) at java.util.Formatter.format(Formatter.java:2520) at java.io.PrintStream.format(PrintStream.java:970) at java.io.PrintStream.printf(PrintStream.java:871) at Game.main(Game.java:6) ```
# 답변입니다 에러는 형식 변환 과정에서 생겼고, 6번째 줄에서 발생했습니다. ``` 획득 금액: $Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302) at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806) at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753) at java.util.Formatter.format(Formatter.java:2520) at java.io.PrintStream.format(PrintStream.java:970) at java.io.PrintStream.printf(PrintStream.java:871) at Game.main(Game.java:6) ``` 6번째 줄을 확인 해보면, 실수형 변수 `won`이 %d로 출력 되고 있습니다. %d는 십진수 출력 즉, 정수형 변수 출력 형식이죠? ``` public class Game { public static void main (String[] args) { int dollar = dice() + dice() + dice(); double won = exchange(dollar); System.out.printf("획득 금액: $%.2f(%d원)", dollar, won); // 에러 발생 위치! } public static int dice(){ int result = (int)(Math.random()*6 + 1); return result; } public static double exchange(int A){ double result = A * 1082.25108; return result; } } ``` 따라서,%f 로 바꾸어 문제를 해결할 수 있습니다. ``` // 변경 전 System.out.printf("획득 금액: $%.2f(%d원)", dollar, won); // 에러 발생! // 변경 후 System.out.printf("획득 금액: $%.2f(%f원)", dollar, won); // 에러 해결! ``` ## PS. 질문 & 피드백 감사합니다!
[해결] 타입 실수 int dollar = dice() + dice() + dice(); double won = exchange(dollar); System.out.printf("획득 금액: $%.2f(%.0f원)", dollar, won); => dollar는 int 타입으로 만들어놓고는 => 출력 시, %f 로 실수했음 (double로 출력 시도) [2차 질문] 6번째 줄에 %d를 => %f로 수정했는데도, 계속 에러가 발생합니다 [코드] public class Game { public static void main (String[] args) { int dollar = dice() + dice() + dice(); double won = exchange(dollar); System.out.printf("획득 금액: $%.2f(%.0f원)", dollar, won); } public static int dice(){ return (int) (Math.random()*6 + 1); } public static double exchange(int A){ return A * 1082.25108; } } [에러] 획득 금액: $Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302) at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806) at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753) at java.util.Formatter.format(Formatter.java:2520) at java.io.PrintStream.format(PrintStream.java:970) at java.io.PrintStream.printf(PrintStream.java:871) at Game.main(Game.java:6)
# dollar 변수의 타입이 int 타입으로 선언했으므로, 출력 형식을 %d로 변경! 또는, 타입을 double로 변경해보세요!