목차
[예제1] 고정 숫자로 덧셈 연산 [예제2] 사용자 입력값 받아 덧셈
※ 예제2처럼, 사용자 입력값 받아 처리하는 게 훨씬 프로그램이 유용.
[예제1] 고정 숫자로 덧셈 연산 Hz.java public class Hz { public static void main(String[] args) { int x = 3; int y = 4; int sum = x + y; System.out.println(sum); // 7 } } 결과값: 7 [예제2] 사용자 입력값 받아 덧셈 Hz.java import java.util.Scanner; public class Hz { public static void main(String[] args) { int x, y, sum; Scanner scan = new Scanner(System.in); System.out.println("숫자 입력:"); x = scan.nextInt(); System.out.println("숫자 입력:"); y = scan.nextInt(); sum = x + y; System.out.println("합계: " + sum); scan.close(); } } 결과값: 사용자 입력값에 따라 다름.
Hz.java
public class Hz {
public static void main(String[] args) {
int x = 3;
int y = 4;
int sum = x + y;
System.out.println(sum); // 7
}
결과값: 7
import java.util.Scanner;
int x, y, sum;
Scanner scan = new Scanner(System.in);
System.out.println("숫자 입력:");
x = scan.nextInt();
y = scan.nextInt();
sum = x + y;
System.out.println("합계: " + sum);
scan.close();
결과값: 사용자 입력값에 따라 다름.
PS.
이클립스에서 Scanner 클래스 가져오기 단축키 : Ctrl + Shift + O