码迷,mamicode.com
首页 > 其他好文 > 详细

随机数

时间:2015-10-31 11:34:45      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

随机数

设计思路:(1)生成随机数(2)将随机数分配到char数组(3)将char数组转换为int类,(4)将随机数添加进string类并计算

源程序代码

import java.util.Random;

import javax.swing.*;

public class Array1 {

       public static void main(String Args[])

       {

              int a[]=new int [10];

              int num = 0;

              String output="";

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

              {

                     a[i]=(int)(Math.random()*100);

              }

              for(int  element:a)

              {

                     output+=element+" ";

                  num+=element;

              }

                     JOptionPane.showMessageDialog( null,"随机数为"+output ,

                       "Initializing an Array with a Declaration",

                       JOptionPane.INFORMATION_MESSAGE );

                     JOptionPane.showMessageDialog( null,"和为"+num ,

                              "Initializing an Array with a Declaration",

                              JOptionPane.INFORMATION_MESSAGE );

                    

             

       }

}

结果截图技术分享技术分享

 

随机数

标签:

原文地址:http://www.cnblogs.com/zuhaoran/p/4925044.html

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