数组课后作业

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

     要求将设计思路、程序流程图、源程序代码、结果截图

 设计思路:定义数组,利用循环结构将产生的随机数【(int)(Math.random()*10)】输入到数组中,最后求和;

 程序流程图:

代码:

package array;

import javax.swing.JOptionPane;

public class ArrayTest {

 

                     int array[]=new int[10];//创建数组

 

                    String output="";       //声明对象

 

                    int sum=0;              

 

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

 

                      {  

 

                        array[i]=(int)(Math.random()*10);//随机产生0~10的随机数

 

                       output+=array[i]+" ";//将数组内容赋值给output

 

                       sum+=array[i];//求和

 

                     }

 

                    output+=" 和为:"+sum;;

 

                    JOptionPane.showMessageDialog(

 

                 null, "生成的随机数为:" + output, "实验结果",

 

                 JOptionPane.PLAIN_MESSAGE );//以消息框形式

}

}

截图:

 

原文地址:https://www.cnblogs.com/chenpengmeng/p/4928483.html