各数相加的思路、流程图、源代码及实现截图

1、大致思路:需要申请一个长度为n的动态数组,其中n为字符个数,然后循环输入,定义一个变量作为和,初始化为0,循环相加,最后输出。

2、流程图

3、源代码:

//2015.9.26 chengchongjing 多数字相加

import java.util.Scanner;

 public class mathplus {

  public static void main(String[] args) {  

 // TODO 自动生成的方法存根     

 Scanner scan= new Scanner(System.in);      

  int n;    

 System.out.println("请输入要相加的字符个数");   

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

 double aArray[];     aArray = new double[n];//定义含n个双精度参数的数组     

    System.out.print("输入"+n);    

      System.out.println("个数字");     

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

  {      

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

       }//把输入的字符串转化成浮点型   

       double result = 0;   

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

    {   

   result +=aArray[i];

    }//循环相加     

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

    }

}

4、运行截图:

原文地址:https://www.cnblogs.com/3066405538a/p/4841533.html