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型別的引數。它允許我們透過終端傳遞引數並將這些引數儲存在一個字串陣列中。我們可以說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()的內建方法也用於新增兩個數字。但是,我們不能像使用“+”運算子那樣使用此內建方法新增多個值。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP