首页 > 学院 > 开发设计 > 正文

随机数组相加

2019-11-14 15:12:39
字体:
来源:转载
供稿:网友

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

import javax.swing.*;

public class Sum {
public static void main(String args[])
{
String output= "10个666666以内的随机数为:/n";
int sum=0;
int a[]=new int [10];
for(int i=0;i<=9;i++)
{
a[i]=(int) (Math.random()*666666);
output += a[i] +",";
sum += a[i];
}
output +="/n十个数的和是:"+sum;

JOptionPane.showMessageDialog(null,output,"结果",JOptionPane.PLAIN_MESSAGE);
}
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表