import java.io.*; class Average 
{
public static void main(String[] arguments)
{
int i,n=3,s=0;
float aver;
try
{
for(i=1;i<=n;i++)
{
System.out.println("s="+s);
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

s=s+Integer.parseInt(br.readLine());
}
}catch (Exception e)
{
System.out.println("输入发生异常");  //经常抛出这个异常,不知道为什么?
}
System.out.println("s="+s);
aver=s/3f;
System.out.println("这10个数的平均值是:"+aver);
}   
}

解决方案 »

  1.   


    catch (Exception e) {
    e.printStackTrace();
    }
      

  2.   

    s=s+Integer.parseInt(br.readLine());
    输入的数据太大了,超出了int型的范围
      

  3.   

    输入三个int型没有异常啊.... 
    除非你输入其他的类型,会报数字格式异常...
      

  4.   

    貌似用readline()读的一行数据哈,连空格、回车符都包括了,而这些是不能转换为Integer的
      

  5.   

    当把你输入的数据转换的时候可能会出java.lang.NumberFormatException,输入的东西都是当成字符串处理,然后再转换成数字举个例子:
    如果你输入: 123           //正常输入:  12g45              //抛出异常,因为你要用Integer.parseInt(br.readLine()),那你输入的字符串就必须是数字
      

  6.   

    /**
         * Parses the string argument as a signed decimal integer. The 
         * characters in the string must all be decimal digits, except that 
         * the first character may be an ASCII minus sign <code>'-'</code> 
         * (<code>'&#92;u002D'</code>) to indicate a negative value. The resulting 
         * integer value is returned, exactly as if the argument and the radix 
         * 10 were given as arguments to the 
         * {@link #parseInt(java.lang.String, int)} method.
         *
         * @param s    a <code>String</code> containing the <code>int</code>
         *             representation to be parsed
         * @return     the integer value represented by the argument in decimal.
         * @exception  NumberFormatException  if the string does not contain a
         *               parsable integer
    .
         */
      

  7.   

    改成下面这样吧,如果在操作系统控制台下输入错误的话还能响铃报警,呵呵。import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;public class Average {    public static void main(String[] arguments) {
            final int N = 3;
            int s = 0;
            try {
                BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
                for(int i = 1; i <= N; i++) {
                    System.out.print("请输入第 " + i + " 个数:");
                    String tmp = br.readLine();
                    if(!tmp.matches("\\d+")) {
                        System.out.println("\7 ** 输入错误,请重新输入 **\n");
                        i--;
                        continue;
                    }
                    System.out.println("您输入的第 " + i + " 个数是:" + tmp + "\n");
                    s += Integer.parseInt(tmp);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("所输入 " + N + " 个数的和为:" + s);
            float aver = s / (float)N;
            System.out.println("这 " + N + " 个数的平均值是:" + aver);
        }
    }