今天查成绩,要算平均数,结果网页没有这个功能就想自己用java实现
成绩如下:
65
69
62
70
67
68
71
60
64
69
74
73
67
71
71
76
70
这些保存在aa.txt文档中我写的代码是:package demo1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;public class AVGStream { public static void main(String[] args) {
try{
File file=new File("F:\\aa.txt");
FileInputStream fis=new FileInputStream(file);
InputStreamReader is=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(is);
String line=null;
// while((line=br.readLine())!=null)
// {
// System.out.println(line);
// }
//判断有多少行,以便生成数组
int n=0;
while((line=br.readLine())!=null)
{
n++;
}
//最终n=17
int[] arr1=new int[n];
//将每一行的值转换为int型保存在数组arr1中
int k=0;
//m从0开始结尾的数应为16
for(int m=0;m<=n-1;m++)
{
//这步有点多余,只要有数值就可以输出
while((line=br.readLine())!=null)
{
int i=Integer.parseInt(br.readLine());
arr1[k]=i;
k++;
}
}
System.out.print(arr1[k]);
}
catch(Exception e){}
}
}先保存在数组中就行,我数组得出结果为0,调试的时候数组生成正确
成绩如下:
65
69
62
70
67
68
71
60
64
69
74
73
67
71
71
76
70
这些保存在aa.txt文档中我写的代码是:package demo1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;public class AVGStream { public static void main(String[] args) {
try{
File file=new File("F:\\aa.txt");
FileInputStream fis=new FileInputStream(file);
InputStreamReader is=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(is);
String line=null;
// while((line=br.readLine())!=null)
// {
// System.out.println(line);
// }
//判断有多少行,以便生成数组
int n=0;
while((line=br.readLine())!=null)
{
n++;
}
//最终n=17
int[] arr1=new int[n];
//将每一行的值转换为int型保存在数组arr1中
int k=0;
//m从0开始结尾的数应为16
for(int m=0;m<=n-1;m++)
{
//这步有点多余,只要有数值就可以输出
while((line=br.readLine())!=null)
{
int i=Integer.parseInt(br.readLine());
arr1[k]=i;
k++;
}
}
System.out.print(arr1[k]);
}
catch(Exception e){}
}
}先保存在数组中就行,我数组得出结果为0,调试的时候数组生成正确
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class AVGStream {
public static void main(String[] args) {
try{
File file=new File("F:\\aa.txt");
FileInputStream fis=new FileInputStream(file);
InputStreamReader is=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(is);
String line=null;
int n = 0;
double total = 0;
while((line=br.readLine())!=null)
{
total += Integer.parseInt(line);
n++;
}
System.out.println(total / n);
}
}想什么呢。。平均数只要总数和个数就行了啊。。
try {
return BigDecimal.valueOf(Double.valueOf(n1))
.add(BigDecimal.valueOf(Double.valueOf(n2)));
} catch (Exception e) {
return null;
}
} public static void main(String... args) {
Scanner scanner = null;
int i = 0;
BigDecimal sum = new BigDecimal(0);
try {
scanner = new Scanner(new FileInputStream("C:\\Users\\vdmdev2\\Desktop\\test1.txt"));
} catch (FileNotFoundException ex) {
System.out.println("invalid input file");
System.exit(1);
}
while (scanner.hasNextLine()) {
try {
sum = Toto.sum(sum.toString(), Double.valueOf(scanner.nextLine()).toString());
i++;
} catch (Exception e) {
System.out.println("invalid input");
System.exit(1);
}
}
System.out.println(sum.divide(new BigDecimal(i), RoundingMode.HALF_DOWN));
}
}