了解如何在 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
}
}
运行示例 »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!