随机数

设计思路:

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

2.用循环语句进行随机数相加,得到相应的结果。

3.调用JOptionPane类输出相应的结果。

流程图:

源程序代码:

import javax.swing.JOptionPane.*;

 

public class number {

 

public static void main(String[] args) {

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

}

 

}

运行结果截图:

posted on 2015-11-01 17:54  宁宁妞  阅读(155)  评论(0编辑  收藏  举报