JAVA数组之课后作业

设计思路:

使用random随机产生10个数,存入数组中;使用for循环把结果存入一个String对象中;使用for循环求出数组中所有元素的和;使用JTextArea和JOptionPane输出数组的内容

程序流程图:

源代码:

import javax.swing.*;

public class Name {
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 );
}
}

截图:

posted @ 2015-11-01 14:33  岩蔷薇之简  阅读(141)  评论(0)    收藏  举报