完整程序参考 public static void main(String[] args) throws Exception { File file = new File("test.txt"); Scanner sc = new Scanner(file); String s; s = sc.next(); //读取一行 int sum=0,ct=0; while(sc.hasNext()) { s=sc.next(); //System.out.println("*"+s); String r = "\\d+"; Pattern p = Pattern.compile(r); Matcher m = p.matcher(s); if (m.find()) { sum+=Integer.valueOf( m.group()) ; ct++; } } System.out.println("平均价="+sum/ct); }