-1
在我的程序中,用掃描儀獲取用戶輸入文件名。用戶在控制檯上輸入.txt文件的名字,我得到了在控制檯輸出以及但是現在如果我想要做這個節目用這種方法:帶方法的文件I/O
public static double deviation(File inputFile)
我如何能實現我的計劃?
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
double number = 0;
double n = 0;
double sum = 0;
double sum1 = 0;
int count = 0;
double mean = 0;
Scanner Scanscan = new Scanner(System.in);
System.out.print("Enter the name of the file: ");
String filename = Scanscan.nextLine();
File inputFile = new File(filename);
Scanner reader = new Scanner(inputFile);
while(reader.hasNext()){
number = reader.nextDouble();
sum = sum + number;
sum1 = sum1 + Math.pow(number,2);
n++;
}
double n1 = Math.sqrt(n);
double sum11 = Math.sqrt(sum1);
mean = sum/n;
double n12 = Math.pow(n,2) - n;
double n22 = Math.pow(sum, 2);
double x = (n * sum1 - n22)/(n12);
double deviation = (Math.sqrt(x));
System.out.println("The standard deviation of the values in this file is: " + deviation);
}
目前還不清楚你在問什麼 - 你在說'我如何將我的代碼重構爲一種方法?' – David