请教个问题,关于JAVA从文件中读取数据的代码问题。
你好,我有个问题,我这里有个求平均数跟标准方差的小程序,我现在是把数据写在程序里面的,我想请教下如果把两组数据放在同一个TXT文件内,怎么从文件里面读取两组数据并求出平均...
你好,我有个问题,我这里有个求平均数跟标准方差的小程序,我现在是把数据写在程序里面的,我想请教下如果把两组数据放在同一个TXT文件内,怎么从文件里面读取两组数据并求出平均数跟标准方差?能帮忙看下吗?谢谢。
public class number {
private double args;
/**
* @param args
*/
public static double avg(double[] args){
double sum=0;
double aver;
for(int i=0;i<args.length;i++)
{
sum=sum+args[i];
}
aver=sum/args.length;
//System.out.println(aver);
return aver;
}
public static double Reusable(double[]args){
double Reusable;
double temp=0;
double sum=0;
for(int i=0;i<args.length;i++)
{
temp=args[i]- avg(args);
sum+=temp*temp;
}
Reusable= Math.sqrt(sum/(args.length-1));
//System.out.println(Reusable);
return Reusable;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
double[] x = new double[] {160,591,114,229,230,270,128,1657 ,624,1503};
double[] y = new double[] {15.0,69.9,6.5,22.4,28.4,65.9,19.4,198.7,38.8,138.2};
System.out.println(avg(x));
System.out.println(Reusable(x));
System.out.println(avg(y));
System.out.println(Reusable(y));
}
} 展开
public class number {
private double args;
/**
* @param args
*/
public static double avg(double[] args){
double sum=0;
double aver;
for(int i=0;i<args.length;i++)
{
sum=sum+args[i];
}
aver=sum/args.length;
//System.out.println(aver);
return aver;
}
public static double Reusable(double[]args){
double Reusable;
double temp=0;
double sum=0;
for(int i=0;i<args.length;i++)
{
temp=args[i]- avg(args);
sum+=temp*temp;
}
Reusable= Math.sqrt(sum/(args.length-1));
//System.out.println(Reusable);
return Reusable;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
double[] x = new double[] {160,591,114,229,230,270,128,1657 ,624,1503};
double[] y = new double[] {15.0,69.9,6.5,22.4,28.4,65.9,19.4,198.7,38.8,138.2};
System.out.println(avg(x));
System.out.println(Reusable(x));
System.out.println(avg(y));
System.out.println(Reusable(y));
}
} 展开
展开全部
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class A{
public static void main(String[] args) throws IOException{
BufferedReader reader=new BufferedReader(new FileReader("d:/data.txt"));
String line1=reader.readLine();
String line2=reader.readLine();
String arr1[]=line1.split(",");
String arr2[]=line2.split(",");
double[] x=new double[arr1.length];
double[] y=new double[arr2.length];
for (int i = 0; i < x.length; i++) {
x[i]=Double.parseDouble(arr1[i]);
}
for (int i = 0; i < y.length; i++) {
y[i]=Double.parseDouble(arr2[i]);
}
}
}
import java.io.FileReader;
import java.io.IOException;
public class A{
public static void main(String[] args) throws IOException{
BufferedReader reader=new BufferedReader(new FileReader("d:/data.txt"));
String line1=reader.readLine();
String line2=reader.readLine();
String arr1[]=line1.split(",");
String arr2[]=line2.split(",");
double[] x=new double[arr1.length];
double[] y=new double[arr2.length];
for (int i = 0; i < x.length; i++) {
x[i]=Double.parseDouble(arr1[i]);
}
for (int i = 0; i < y.length; i++) {
y[i]=Double.parseDouble(arr2[i]);
}
}
}
参考资料: 还有其他问题的话,给我发百度消息
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询