多个数字求和

1.设计思想

      从命令行内输入多个参数,先把输入的参数遍历一遍计算出参数的个数count,然后申请一个大小为count的double型数组用来存放转化为double型的参数。最后将double类型的参数相加求和并输出。

2.流程图

 

 

3.源程序代码


public class Sum {
 public static void main(String[] args) {
   int count=0;//count是记录共输入了几个参数
   for (String arg : args) {
  count++;
   }
   double[] a=new double[count];//a[count]是存放参数转化为int的类型的数组
   double sum=0;//sum是所求和
   int i=0;
  
   for(String arg:args) {
    a[i]=Double.valueOf(arg.toString());
    sum+=a[i];
    if(i==count-1)
     System.out.print(a[i]);
    else
     System.out.print(a[i]+"+");
    i++;
   }
   System.out.println("="+sum);
 }
 
}

结果截图

 

 

posted @ 2017-09-29 08:25  我是一个粉刷匠^~^  阅读(718)  评论(0编辑  收藏  举报