java数组元素相加_数组元素相加
設(shè)計思路:
1.產(chǎn)生隨機數(shù)放在數(shù)組中;
2.循環(huán)相加計算得到最終結(jié)果
3.用JOptionPane.showMessageDialog顯示結(jié)果
流程圖:
源程序代碼:
import?javax.swing.*;
public?class?Suijishu?{
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
int[]?arr=new?int[10];//定義數(shù)組
String?output="";
int?j=0;
output+="Subscript\tValue\n";
for(int?i=0;i<10;i++)
{
arr[i]=(int)(Math.random()*100);//隨機產(chǎn)生0-100范圍內(nèi)的整數(shù)
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,"數(shù)組元素",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,j,"數(shù)組元素的和",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
運行結(jié)果截圖:
總結(jié)
以上是生活随笔為你收集整理的java数组元素相加_数组元素相加的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 取字符的ascii_如何用ja
- 下一篇: java list断点续传_java实现