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);
}
}
{
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);
}
}
catch (Exception e) {
e.printStackTrace();
}
输入的数据太大了,超出了int型的范围
除非你输入其他的类型,会报数字格式异常...
如果你输入: 123 //正常输入: 12g45 //抛出异常,因为你要用Integer.parseInt(br.readLine()),那你输入的字符串就必须是数字
* 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>'\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.
*/
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);
}
}