Algorithm Study
[Java Algorithm] ํ๋ก๊ทธ๋๋จธ์ค Lv.0 _ ๋ง์ ์ ์ถ๋ ฅํ๊ธฐ
microsaurs
2024. 3. 6. 14:47
๐ฟ ๋ฌธ์ ๐ฟ
๋ ์ ์ a, b ๊ฐ ์ฃผ์ด์ง ๋ ๋ค์๊ณผ ๊ฐ์ ํํ์ ๊ณ์ฐ์์ ์ถ๋ ฅํ๋ ์ฝ๋๋ฅผ ์์ฑํด๋ณด์ธ์
a + b = c
[Algorithm]
1. ์ฐ์ฐ์๋ฅผ ๋ฌธ์์ด๋ก ํํ
2. System.out.printf ์ฌ์ฉํด์ ํํ
[Code]
import java.util.Scanner;
public class Solution1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = a+b;
System.out.println(a + " + " + b + " = " + c );
}
}
public class Solution2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.printf("%d + %d = %d",a,b,a+b);
}
}
[+ Plus]
โญ๏ธ Java์ ์ถ๋ ฅ๋ฌธ
1. System.out.println()
โก๏ธ ๊ดํธ ์์ ๊ฐ์ ๊ทธ๋๋ก ์ถ๋ ฅ + ๋ง์ง๋ง์ ์ค๋ฐ๊ฟ์ ๋ฃ์ด์ฃผ๋ ๋ฉ์๋
2. System.out.printf()
โก๏ธ ์์๋ฌธ์์ด์ ์ฌ์ฉํ์ฌ ์ถ๋ ฅ
printf("์์๋ฌธ์์ด", value) ํ์์ผ๋ก ์ฌ์ฉ
* ์์๋ฌธ์
- %d : ์ ์ - %f : ์ค์ - %s : ๋ฌธ์์ด - %c : ๋ฌธ์
printf๋ ์๊ฐ์ ๋ชปํ๋๋ฐ, , , ๋๋๊ธฐ๋ค