Java 添加两个数字

Java 添加两个数字

当我们进入编码世界时,我们通过编程语言学习了各种数学运算。我们期望以基本的数学运算(如加法、减法、乘法和除法)开始我们的编码之旅。要计算两个数字的和,我们使用’+’运算符。然而,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

通过在声明时初始化值

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

示例

下面的示例演示了我们上面讨论的实际实现。

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

使用sum()方法

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

示例

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

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[] 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()的内建方法也用于两个数相加。但是,我们不能使用这个内建方法像“+”运算符一样同时相加多个值。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程