码迷,mamicode.com
首页 > 编程语言 > 详细

数组元素相加

时间:2015-10-31 17:08:25      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

设计思路:

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

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

3.用JOptionPane.showMessageDialog显示结果

流程图:

技术分享

源程序代码:

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);

}

 

}

运行结果截图:

技术分享技术分享

 

数组元素相加

标签:

原文地址:http://www.cnblogs.com/11-05/p/4925673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!