Rss & SiteMap

课外天地 李树青 http://www.njcie.com

李树青 论坛 南京 财经 课外天地
共1 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:程序代码——要求用户输入5个数,最后打印平均值

1楼
admin 发表于:2006/2/26 21:30:56

使用数组的做法:
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编辑过]
共1 条记录, 每页显示 10 条, 页签: [1]

Copyright ©2002 - 2016 课外天地.Net
Powered By Dvbbs Version 8.3.0
Processed in .03125 s, 2 queries.