Java程序如何相加两个数字

时间:2020-02-23 14:35:06  来源:igfitidea点击:

在本教程中,我们将看到如何在Java中相加两个数字。
这是Java编程语言最基本的程序。

import java.util.Scanner;
public class Calculator
{
    public static void main(String args[])
    {
        int num1, num2, sum;
        //Taking input from user
        Scanner scanner = new Scanner(System.in);
		
        System.out.print("Enter two numbers : ");
        num1 = scanner.nextInt();
        num2 = scanner.nextInt();
	
        //Adding two numbers	
        sum = num1 + num2;
		
        System.out.print("Sum of two numbers is " +sum);
    }
}

输出:

Enter two numbers : 5 7
Sum of two numbers is 12