课外天地 李树青学习天地Java程序语言课件 → 程序代码——要求用户输入5个数,最后打印平均值


  共有16939人关注过本帖树形打印复制链接

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

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——要求用户输入5个数,最后打印平均值  发帖心情 Post By: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编辑过]

 回到顶部