随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中。

设计思路:先申请一个长度为十的数组,再运用for循环和调用函数依次为元素赋值,输出数组,用for循环,将元素依次相加求和,输出结果。

程序流程图:

源程序:

//胡泽杰 2015.10

import javax.swing.*;

public class Number{

  public static void main( String args[] ){

   int num=0;

  String output;

  int n[] = new int[10];

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

   n[i]=(int)(Math.random()*1000);

}

  output ="随机生成的十个数为:"+"\n";

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

  output+=n[i]+" ";

  output+="\n";

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

  num +=n[i];

  output +="这十个数相加等于:"+num;

   JOptionPane.showMessageDialog( null, output,
  "shuchu",
  JOptionPane.INFORMATION_MESSAGE );

  }

}

运行结果:

 

posted on 2015-11-01 20:06  胡泽杰  阅读(117)  评论(0编辑  收藏  举报