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

数组--随机数

时间:2015-11-03 00:31:53      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

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

设计思路:使用random随机产生10个数,使用for循环把结果存入一个String对象中,使用JTextArea和JOptionPane输出数组的内容

程序流程图

技术分享

源代码:

import javax.swing.*;

public class Lianxi { public static void main( String args[] ) { String output = ""; int num=0;

int n[] = new int[10]; for(int i=0;i<n.length;i++){ n[i]=(int)(Math.random()*100+1); }

output += "Subscript\tValue\n";

for ( int i = 0; i < n.length; i++ ){ output += i + "\t" + n[ i ] + "\n"; num+=n[i]; }

JTextArea outputArea = new JTextArea( 11, 10 ); outputArea.setText( output );

JOptionPane.showMessageDialog( null, outputArea, "Initializing an Array with a Declaration", JOptionPane.INFORMATION_MESSAGE ); JOptionPane.showMessageDialog(null,num,"数组中所有元素的和:", JOptionPane.INFORMATION_MESSAGE);

System.exit( 0 ); } }

结果截图:技术分享

技术分享技术分享技术分享

数组--随机数

标签:

原文地址:http://www.cnblogs.com/lukaner/p/4931947.html

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