程序从命令行接收多个数字,求和之后输出结果

设计思想:获取输入数的个数,存入新建的一个动态数组,然后将动态数组内的数利用Integer.parseInt()函数从string转为int,相加输出。

程序流程图:获取输入数的个数->新建动态数组->将输入数存入动态数组->建立int变量s=0->利用Integer.parseInt()函数转换并相加->输出;

源程序代码:

import java.util.ArrayList;

public class D1

  @SuppressWarnings({ "unchecked", "rawtypes" })

   public static void main(String[] args) 

  { 

    int a;  

    a=args.length;//获取输入的数字个数  

    ArrayList a1=new//建立动态数组  

    ArrayList();   for(int i=0;i<a;i++)//将输入的数字存进动态数组   

      a1.add(args[i]);  

    int s=0;  

    for(int i = 0;i<a;i++)  

    {   

      String b=(String)a1.get(i);   

      int c;   

      c=Integer.parseInt(b);//将String转为int型   

      s=s+c;//相加求和  

    }  

    System.out.println(s);//输出 

  }

}

程序截图:

原文地址:https://www.cnblogs.com/LJT666/p/5532413.html