문제 설명
정수 a
와 b
가 주어집니다. 각 수를 입력받아 입출력 예와 같은 형식으로 출력하는 코드를 작성해 보세요.
제한사항
- -100,000 ≤
a
,b
≤ 100,000
입출력 예
입력 #1
4 5
출력 #1
a = 4
b = 5
정수 a
와 b
가 주어집니다. 각 수를 입력받아 입출력 예와 같은 형식으로 출력하는 코드를 작성해 보세요.
a
, b
≤ 100,000입력 #1
4 5
출력 #1
a = 4
b = 5
문자열 출력하기를 먼저 풀어보세요!
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on('line', function (line) {
input = line.split(' ');
}).on('close', function () {
console.log(Number(input[0]) + Number(input[1]));
});
split()
함수를 사용하여 공백을 기준으로 문자열을 나눠 배열에 저장할 수 있어요.
따라서 예를 기준으로 input
에는 ['4', '5']
가 저장되어 있어요.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on('line', function (line) {
input = line.split(' ');
}).on('close', function () {
const [a, b] = input;
});
a와 b에 각각 input[0]
과 input[1]
을 저장해두어요.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on('line', function (line) {
input = line.split(' ');
}).on('close', function () {
const [a, b] = input;
console.log(`a = ${a}`);
console.log(`b = ${b}`);
});
console.log()
함수를 사용하여 출력할 수 있어요.
문자열과 변수를 함께 출력하는 방법은 두 가지가 있어요.
console.log('a = ' + a);
console.log(`a = ${a}`);
첫 번째 방법은 문자열과 변수를 +
로 연결하는 방법이에요.
두 번째 방법은 문자열을 `
(백틱)으로 감싸고 변수를 ${}
로 감싸는 방법이에요.
여기에서는 두 번째 방법을 사용했어요.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
rl.on('line', function (line) {
input = line.split(' ');
}).on('close', function () {
const [a, b] = input;
console.log(`a = ${a}`);
console.log(`b = ${b}`);
});