所以我正在完成一项任务,我完成了大部分工作。不幸的是,实施最终方法并不成功。我不确定我是不是正确地初始化变量或者是什么。如果我单独测试每个其他方法,那么它将显示我需要它的确切内容但是让displaySalesInfo方法工作似乎超出了我的范围。任何建议都会非常感激。
public class SalesTracking {
public static void main(String[] args) {
// declare arrays
String[] monthArray = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
double[] monthlySales = new double[12];
int highestMonth = computeHighestMonth(monthlySales), lowestMonth = computeLowestMonth(monthlySales);
double totalSales = computeTotalSales(monthlySales), averageSales = computeAverageSales(monthlySales), highestSales = monthlySales[highestMonth], lowestSales = monthlySales[lowestMonth];
//call methods
getSales(monthArray, monthlySales);
//computeTotalSales(monthlySales);
//computeAverageSales(monthlySales);
//computeHighestMonth(monthlySales);
//computeLowestMonth(monthlySales);
displaySalesInfo(totalSales, averageSales, highestMonth, highestSales, lowestMonth, lowestSales);
} //end main
public static void getSales(String monthArray[], double monthlySales[]) {
//prepare for input
Scanner input = new Scanner(System.in);
//input sales
for(int i = 0; i < monthlySales.length; i++) {
//System.out.print("Enter sales for " + monthArray[i] + ": ");
monthlySales[i] = input.nextDouble();
} //end for
} //end getSales
public static double computeTotalSales(double monthlySales[]) {
double totalSales = 0;
for(int i = 0; i < monthlySales.length; i++) {
totalSales += monthlySales[i];
} //end for
//System.out.println("Yearly Total: " + totalSales);
return totalSales;
} //end computeTotalSales
public static double computeAverageSales(double monthlySales[]) {
double total = 0;
for(int i = 0; i < monthlySales.length; i++) {
total += monthlySales[i];
} //end for
double averageSales = total / monthlySales.length;
//System.out.println("Average Sales: " + averageSales);
return averageSales;
} //end computeAverageSales
public static int computeHighestMonth(double monthlySales[]) {
int highestMonth = 0;
double highestSales = monthlySales[0];
for(int i = 0; i < monthlySales.length; i++) {
double maxNumber = monthlySales[i];
if(maxNumber > monthlySales[highestMonth]) {
highestMonth = i;
} //end if
if(highestSales < monthlySales[i]) {
highestSales = monthlySales[i];
} //end if
} //end for
//System.out.println("The highest month is: " + highestMonth);
//System.out.println("The highest sales amount was: " + highestSales);
return highestMonth;
} //end computeHighestMonth
public static int computeLowestMonth(double monthlySales[]) {
int lowestMonth = 11;
double lowestSales = monthlySales[0];
for(int i = 0; i < monthlySales.length; i++) {
double minNumber = monthlySales[i];
if(minNumber < monthlySales[lowestMonth]) {
lowestMonth = i;
} //end if
if(lowestSales > monthlySales[i]) {
lowestSales = monthlySales[i];
} //end if
} //end for
//System.out.println("The lowest month is: " + lowestMonth);
//System.out.println("The lowest sales amount was: " + lowestSales);
return lowestMonth;
} //end computeLowestMonth
public static void displaySalesInfo(double totalSales, double averageSales, int highestMonth, double highestSales, int lowestMonth, double lowestSales) {
System.out.println("Yearly Total: " + totalSales);
System.out.println("Average Sales: " + averageSales);
System.out.println("The highest month is: " + highestMonth);
System.out.println("The highest sales amount was: " + highestSales);
System.out.println("The lowest month is: " + lowestMonth);
System.out.println("The lowest sales amount was: " + lowestSales);
} //end displaySaleInfo
} //end class
答案 0 :(得分:0)
在计算其他数字之前,您必须先致电getSales(monthArray, monthlySales);
。
getSales(monthArray, monthlySales); // call this before otheres
int highestMonth = computeHighestMonth(monthlySales), lowestMonth = computeLowestMonth(monthlySales);
double totalSales = computeTotalSales(monthlySales), averageSales = computeAverageSales(monthlySales), highestSales = monthlySales[highestMonth], lowestSales = monthlySales[lowestMonth];
你的编程技巧很糟糕。可读性较差。我推荐以下代码。
// declare arrays
String[] monthArray = { "January", "February", "March", "April", "May", "June", "July", "August", "September",
"October", "November", "December" };
double[] monthlySales = new double[12];
getSales(monthArray, monthlySales);
int highestMonth = computeHighestMonth(monthlySales);
int lowestMonth = computeLowestMonth(monthlySales);
double totalSales = computeTotalSales(monthlySales);
double averageSales = computeAverageSales(monthlySales);
double highestSales = monthlySales[highestMonth];
double lowestSales = monthlySales[lowestMonth];