这是一个编程书籍的例子,我了解,但是我面临一个大问题:MonthlySales.txt尚未由本书作者建立和解释。在节目结束了就是被问道:输入包含销售金额30天文件的名称:MonthlySales.txt我不明白这个Java程序
The total sales for the period is : 56.437.42
The average daily sales were : 1881.25
这里的程序:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class SaleReport {
public static void main(String[] args) throws IOException {
final int NUMDAYS = 30 ;
String fileName ;
double totalSales ;
double averageSales ;
fileName = getFileName();
totalSales = getTotalSales(fileName);
averageSales = totalSales/NUMDAYS ;
displayResults(totalSales, averageSales);
System.exit(0);
}
public static String getFileName()
{
String file ;
file = JOptionPane.showInputDialog(" Enter the file name for 30 days sales ");
return file ;
}
public static double getTotalSales(String fileName) throws IOException
{
double total = 0.0 ;
double sales ;
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
while(inputFile.hasNextLine())
{
sales = inputFile.nextDouble();
sales += sales ;
}
inputFile.close();
return total ;
}
public static void displayResults(double total , double avg)
{
JOptionPane.showMessageDialog(null,String.format(" The total sales for the period is $%,.2f\n"+
"The daily average sales were $%,.2f ", total, avg ));
}
}
我从来没有处理java,但我认为这个代码只是计算你输入的文件的行数 –
我认为他们缺少一条线。 Total总是0.0不是吗? –
我认为你必须改变销售+ =销售总额+ =销售 –