Java 두 개의 숫자를 추가하는 방법
두 개의 숫자 더하기
Java에서 두 개의 숫자를 추가하는 방법을 알아보세요.
예시
int x = 5;
int y = 6;
int sum = x + y;
System.out.println(sum); // Print the sum of x + y
사용자 입력으로 두 개의 숫자 더하기
사용자 입력으로 두 개의 숫자를 추가하는 방법을 알아보세요.
예시
import java.util.Scanner; // Import the Scanner class
class MyClass {
public static void main(String[] args) {
int x, y, sum;
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Type a number:");
x = myObj.nextInt(); // Read user input
System.out.println("Type another number:");
y = myObj.nextInt(); // Read user input
sum = x + y; // Calculate the sum of x + y
System.out.println("Sum is: " + sum); // Print the sum
}
}