数组随机数求和

作业说明

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

要求将设计思路、程序流程图、源程序代码、结果截图、编程总结等发表到博客园。

一、程序设计思路

使用Random类,产生随机数;使用JOptionPane类,使用消息框,输出信息;

创建一个int型数组,长度为10,存放随机数;

使用for循环把随机数存入数组中并求出数组中所有元素的和

使用JTextArea和JOptionPane生成消息框,输出数组的求和结果

二、程序流程图

三、程序源代码

//随机数组元素求和 信1403-220142976蒋保宁 参考资料 InitArray.java
package demo;
import javax.swing.*;
public class Qiuhe {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        String output = "";
        int num=0;
        int array[] = new int[10];
        for(int i=0;i<array.length;i++){
        array[i]=(int)(Math.random()*100+1);
        }

        output += "序列号	数组元素
";

        for ( int i = 0; i < array.length; i++ )
        { 
        output += i + "	" + array[ i ] + "
";
        num+=array[i];
        }
        JTextArea outputArea = new JTextArea( 11, 10 );
          outputArea.setText( output );

          JOptionPane.showMessageDialog( null, outputArea,"系统产生的数组随机数:",
             JOptionPane.INFORMATION_MESSAGE );
          JOptionPane.showMessageDialog(null,"数组产生的随机数的和为:"+num);

          System.exit( 0 );

    }

}

四、结果截图

五、编程总结

随机数的产生:使用Random类,产生随机数

JTextArea outputArea = new JTextArea( 11, 10 );
outputArea.setText( output );将一个JTextArea嵌入到JOptionPane中

使用 实现字符串的分列输出的

原文地址:https://www.cnblogs.com/sist1437493141/p/4920090.html