n个数求和

一.程序设计思想

使用Integer.parseInt(arg)即可。使用args.length可以得出参数的个数,使用for循环,当args中存在参数时,输出该参数,并使用类型转换进行求和。

二.源程序代码

     package Test;

import java.util.Scanner;

public class Test

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in); //创建Scanner类对象

        int n;

        System.out.println("请输入需要几个数字求和:");

        n=sc.nextInt(); //输入要求和的数的数量

        double[] m;

        m = new double[n];//定义n个双精度参数

        System.out.print("请输入"+n+"个数字");

        for(int i=0;i<m.length;i++)

        {

            m[i]=sc.nextInt();//输入n个求和的数

        }

        double result=0;

        for(int i=0;i<m.length;i++)

        {

            result += m[i];

        }

        

        System.out.println("相加结果为:"+ result);

    }

}

三。程序设计流程图

                

 四。结果截图

原文地址:https://www.cnblogs.com/877612838zzx/p/7634689.html