随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中。
要求将设计思路、程序流程图、源程序代码、结果截图、编程总结等发表到博客园。 (截止时间2015-11-2晚24:00)
package 程序;
import javax.swing.*;
public class ShowArrays
{
public static void main(String[] args)
{
int Array[] = new int[10];
for(int i = 0;i < 10;i++)
{
int random = (int) (Math.random() * 100);//产生一个1到100的随机整数
Array[i] = random;
}
int sum = 0;
for(int i = 0;i < 10;i++)
sum = sum + Array[i];//数组求和
String output = new String("数组元素为 "+Array[0]+" "+Array[1]+" "+Array[2]+" "+Array[3]
+" "+Array[4]+"\n"+Array[5]+" "+Array[6]+
" "+Array[7]+" "+Array[8]+" "+Array[9]+"\n"+"数组元素之和为\t"+sum);
JOptionPane.showMessageDialog(null,output,"随机生成数组",JOptionPane.INFORMATION_MESSAGE);
}
}
package 程序;
import javax.swing.*;
public class tt
{
public static void main(String[] args)
{
// TODO 自动生成的方法存根
String Shuru,Shuchu = "";//用户输入的数字和最后转化成的汉语
String chineseNum[] = {"一","二","三","四","五","六","七","八","九"};
String chineseCharacter[] = {"百亿","十亿","亿","千万","百万","十万","万","千","百","十",""};
Shuru = JOptionPane.showInputDialog(null, "请输入一个整数","欢迎使用本程序" ,3);
int ShuruNum = Integer.valueOf(Shuru);//把用户输入的字符串转化为整数
int getNum[] = new int[Shuru.length()],i = 0;//存放用户输入的各位数,局部变量初始化
while(true)
{
getNum[i] = (ShuruNum % 10);
i++;
ShuruNum = ShuruNum/10;
if(ShuruNum == 0)
break;
}//从高到低取出各个数字并存放在getNum[]当中现在的数组存放的是当前数字的逆序
int length = getNum.length;//求出ShuruNum的长度,这决定了从哪一位开始读数
for(i = 0;i < length;i++)
{
int index;
index = getNum[i];
getNum[i] = getNum[length -1 - i];
getNum[length - 1 - i] = index;
}//把这个逆序的数组再转换过来
for(i = 0;i < length;i++)
{
Shuchu += chineseNum[getNum[i] - 1];
Shuchu += chineseCharacter[chineseCharacter.length - length + i];
}
JOptionPane.showMessageDialog(null, Shuchu, "转化成汉语的结果是", 1);
}
}