java数组元素相加_数组元素相加

设计思路:1.产生随机数放在数组中;2.循环相加计算得到最终结果3.用JOptionPane.showMessageDialog显示结果流程图:源程序代码:im

设计思路:

1.产生随机数放在数组中;

2.循环相加计算得到最终结果

3.用JOptionPane.showMessageDialog显示结果

流程图:

c6e39ffa210801d0b562159ea2c5b350.png

源程序代码:

import javax.swing.*;

public class Suijishu {

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] arr=new int[10];//定义数组

String output="";

int j=0;

output+="Subscript\tValue\n";

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

{

arr[i]=(int)(Math.random()*100);//随机产生0-100范围内的整数

output+=i+"\t"+arr[i]+"\n";

}

JTextArea outputArea=new JTextArea(11,10);

outputArea.setText(output);

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

{

j+=arr[i];

}

JOptionPane.showMessageDialog(null,outputArea,"数组元素",JOptionPane.INFORMATION_MESSAGE);

JOptionPane.showMessageDialog(null,j,"数组元素的和",JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

运行结果截图:

c3b1fb8595ecc7cc144a563b556784e8.png

c844adbb1b710ad9fa52f0b0d723dafa.png