Java程序添加两个数字


当我们进入编码世界时,我们会学习通过编程语言进行各种数学运算。我们期望从基本的数学运算开始我们的编码之旅,例如加法、减法、乘法和除法。为了计算两个数字的总和,我们使用“+”运算符。但是,Java也提供了其他方法来添加两个数字。本文旨在探讨在Java中添加两个数字的各种可能方法。

为了在Java中添加两个数字,我们将使用以下方法:

  • 从用户处获取操作数的输入

  • 在声明时初始化值

  • 使用sum()方法

  • 使用命令行参数

让我们逐一讨论它们。

通过从用户处获取操作数的输入来添加两个数字

要从键盘获取输入,我们需要创建一个Scanner类的实例,该类提供各种用于用户输入的内置方法。例如,如果我们需要输入一个整数值,则可以使用'nextInt()'方法

语法

Scanner nameOfinstance = new Scanner(System.in);

示例

在下面的示例中,我们将使用Scanner类接受两个整数类型的操作数,以执行它们之间的加法运算。

import java.util.Scanner; public class NumberAddition { public static void main(String[] args) { int input_1, input_2, my_sum; // Scanner to read input from user Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.println("Enter the first number: "); // to take first operand input_1 = my_scanner.nextInt(); System.out.println("Enter the second number: "); // to take second operand input_2 = my_scanner.nextInt(); my_scanner.close(); System.out.println("The scanner object has been closed"); // calculating and storing the result my_sum = input_1 + input_2; System.out.println("Sum of the two numbers is: "); // to print the result System.out.println(my_sum); } }

输出

A reader object has been defined 
Enter the first number: 
55
Enter the second number: 
44
The scanner object has been closed
Sum of the two numbers is: 
99

在声明时初始化值来添加两个数字

这是添加两个数字最简单的方法。我们只需要声明两个操作数并用我们选择的值初始化它们。此外,我们需要一个第三个变量来存储加法的结果。

示例

以下示例说明了我们上面讨论内容的实际实现。

Open Compiler
public class NumberAddition { public static void main(String[] args) { // declaring and initializing operands int value_1, value_2, my_sum; value_1 = 10; value_2 = 15; System.out.println("The two operands are: " + value_1 + " and " + value_2); // adding the values my_sum = value_1 + value_2; System.out.println("Sum of the given two numbers is : "); // printing the result System.out.println(my_sum); } }

输出

The two operands are: 10 and 15
Sum of the given two numbers is : 
25

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

使用sum()方法添加两个数字

sum()方法是Integer类的一个静态方法,它接受两个整数类型的操作数作为参数并返回这两个操作数的和。请注意,它是一个静态方法,因此我们需要使用Integer类来调用它。

示例

以下示例演示了如何使用sum()方法添加两个数字。

Open Compiler
public class NumberAddition { public static void main(String[] args) { // declaring and initializing operands int value_1, value_2, my_sum; value_1 = 10; value_2 = 15; System.out.println("The two operands are: " + value_1 + " and " + value_2); // adding the values my_sum = Integer.sum(value_1, value_2); System.out.println("Sum of the given two numbers is : "); // printing the result System.out.println(my_sum); } }

输出

The two operands are: 10 and 15
Sum of the given two numbers is : 
25

使用命令行参数添加两个数字

String[] args是Java main()方法的一个参数,它接受String类型的参数。它允许我们通过终端传递参数并将这些参数存储在一个字符串数组中。我们可以说String[] args是一个命令行参数。

示例

在这个示例中,我们将使用命令行获取两个double类型的操作数并计算它们的和。

public class Addition { public static void main(String[] args) { // to take input from terminal double operand1 = Double.parseDouble(args[0]); double operand2 = Double.parseDouble(args[1]); // adding the inputs double my_sum = operand1 + operand2; // to print the result System.out.println("Sum of the two numbers: " + my_sum); } }

输出

PS D:\Java Programs> javac Addition.java
PS D:\Java Programs> java Addition 9.23 9.32
Sum of the two numbers : 18.55

结论

在本文中,我们学习了添加两个数字的不同方法。最简单和最常用的方法是使用“+”运算符。还有一个名为sum()的内置方法也用于添加两个数字。但是,我们不能像使用“+”运算符那样使用此内置方法添加多个值。

更新于:2024年6月21日

10K+浏览量

开启你的职业生涯

通过完成课程获得认证

开始学习
广告