-- 作者:admin
-- 发布时间:2006/2/26 21:30:56
-- 程序代码——要求用户输入5个数,最后打印平均值
使用数组的做法: public class exec { public static void main( String args[] ) throws Exception { int[] numarray=new int[5]; for(int i=0;i<numarray.length;i++) { String s=javax.swing.JOptionPane.showInputDialog("Please input the number("+(i+1)+"/"+numarray.length+")"); numarray=Integer.parseInt(s); } int amount=0; for(int i=0;i<numarray.length;i++) amount=amount+numarray; String str=String.valueOf((double)amount/numarray.length); javax.swing.JOptionPane.showMessageDialog(null,str); System.exit(0); } }
不使用数组的做法: public class exec { public static void main( String args[] ) throws Exception { int amount=0; final int times=5; for(int i=0;i<times;i++) { String s=javax.swing.JOptionPane.showInputDialog("Please input the number("+(i+1)+"/"+times+")"); amount=amount+Integer.parseInt(s); } String str=String.valueOf((double)amount/times); javax.swing.JOptionPane.showMessageDialog(null,str); System.exit(0); } }
[此贴子已经被作者于2010-12-12 07:37:25编辑过]
|