public class Ex_3_1_0 {
public static void main(String[] args) {
int a = 10;
int b =4;
System.out.printf ("%d + %d = %d%n", a, b, a+b);
System.out.printf ("%d - %d = %d%n", a, b, a-b);
System.out.printf ("%d * %d = %d%n", a, b, a*b);
System.out.printf ("%d / %d = %d%n", a, b, a/b);
System.out.printf ("%d % %d = %d%n", a, b, a%b);
}
}
이렇게 출력했더니 무수히 많은 에러가.. 나머지 연산 하려면 어떻게 해야될까?
printf를 이용한 %때문에 에러가 생기는거같은데
보통 출력되지 않는 문자는 앞에 (백슬래시)를 붙이면 되던데
얘는 안되더라..
println이나 print를 사용해야만 되는걸까? printf로는 정녕 나머지 연산을 할 수 없단말인가..
라고 고민할 때 구글링을 해보았다.
https://www.inflearn.com/questions/186155
명쾌한 답변이 적혀있다
printf에서 %(퍼센트 기호)는 포맷 출력을 위한 특수한 의미로 사용되기 때문에 printf에선 %를 두번 입력(%%)해야 %가 출력된다.
public class Ex_3_1_0 {
public static void main(String[] args) {
int a = 10;
int b =4;
System.out.printf ("%d + %d = %d%n", a, b, a+b);
System.out.printf ("%d - %d = %d%n", a, b, a-b);
System.out.printf ("%d * %d = %d%n", a, b, a*b);
System.out.printf ("%d / %d = %d%n", a, b, a/b);
System.out.printf ("%d %% %d = %d%n", a, b, a%b);
}
}
출력값: 10 + 4 = 14
10 - 4 = 6
10 * 4 = 40
10 / 4 = 2
10 % 4 = 2
그렇다고 합니다. 해결~