码迷,mamicode.com
首页 > 编程语言 > 详细

java 加法运算

时间:2019-12-25 13:01:23      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:find   static   for   ber   port   system   ati   close   enter   

 

//find sum of two numbers

import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {
        int num1, num2, sum;
        Scanner ip = new Scanner(System.in);
        System.out.print("Enter num1: ");
        num1 = ip.nextInt();
        System.out.print("Enter num2: ");
        num2 = ip.nextInt();
        sum = num1 + num2;
        System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum);
        ip.close();
    }

}



OUTPUT:
Enter num1: 35
Enter num2: 45
The sum of 35 and 45 is 80

 2

//find sum of n numbers

import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {
        int n, temp, sum = 0;
        Scanner ip = new Scanner(System.in);
        System.out.print("Enter the total number of input: ");
        n = ip.nextInt();
        for (int i = 1; i <= n; i++) {
            System.out.print("Enter input " + i + ": ");
            temp = ip.nextInt();
            sum = sum + temp;
        }
        System.out.println("The sum of all input numbers is: " + sum);
        ip.close();
    }

}



OUTPUT:
Enter the total number of input: 5
Enter input 1: 6
Enter input 2: 3
Enter input 3: 8
Enter input 4: 2
Enter input 5: 9
The sum of all input numbers is: 28

 

java 加法运算

标签:find   static   for   ber   port   system   ati   close   enter   

原文地址:https://www.cnblogs.com/sea-stream/p/12095997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!