编写多个数字相加的java程序

设计思想:eclipse来写从命令行接收多个数字的程序首先输入要通过new Scanner(System.in)创建一个Scanner,然后在控制台输入几个整数,输入完毕后,将内容传递给Scanner,因为输入的整数为字符类型,所以要先把字符转化为整数类型,则需要调用ScannernextInt()函数来完成转化。最后求和并输出。

程序流程图:

 

程序源代码:

//把用户输入的数字求和并输出

//范亚雷   20150926

import java.util.Scanner;

public class Addnumber {

public static void main(String args[]){

System.out.println("请输入五个整数:");

Scanner sc = new Scanner(System.in);   //为输入的整数申请空间

//将字符转化为整数

int a = sc.nextInt();          

int b = sc.nextInt();

int c = sc.nextInt();

int d = sc.nextInt();

int e = sc.nextInt();

int f = a + b + c + d + e;    //求和

System.out.println("这五个数的和为:" + f ); //输出结果

}

}

程序源代码截图:

 

程序运行结果截图:

原文地址:https://www.cnblogs.com/fan-xiaofan/p/4841165.html