我被困在某事上,如果我弄明白,我可以完成剩下的学业任务。我无法将数组值返回到main。我使用readData()
方法将文本文件中的double值读入我的数组double [] miles
。如果我使用for循环在readData()方法内打印它会打印值,但如果我在方法调用下的main()
中使用它,它不会打印出值。我在这做错了什么?程序可以编译和运行但不完整。欢迎提出所有问题,意见,疑虑和建议。请帮忙
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
public class CollinDunn_1_08 {
static Toolkit tools = new Toolkit();
public static void main (String [] args) throws IOException {
//I/O String references
final String INPUT_FILE = "CollinDunn_1_08_Input.txt" ;
final String OUTPUT_FILE = "CollinDunn_1_08_Output.txt";
//Variable and Array Declarations
int numValues = 0;
double [] miles = new double [0];
double [] payMe = new double [0];
// These are string references for methods
String genInfo = "";
String headInfo = "";
String outInfo = "";
//Access Input and Output
FileInputStream inputDataFile = new FileInputStream(INPUT_FILE);
Scanner inputFile = new Scanner(inputDataFile);
FileWriter outputDataFile = new FileWriter(OUTPUT_FILE);
PrintWriter outputFile = new PrintWriter(outputDataFile);
// Initiate program
System.out.println("Initiating program.");
// progInfo method, this displays
// the genral info about the program
progInfo(outputFile, genInfo);
// readData method, this reads the
// mileage values from the file and
// stores in in the miles array.
readData(miles, inputFile, numValues);
for(int i = 0; i < numValues; i++) {
System.out.print(miles[i] + "\n");
}
//Calculate reimbursements
printHeading(outputFile, headInfo);
payCalc(miles, payMe, numValues);
} // End main
//This method reads the mile data from a file and stores it
public static double [] readData(double [] miles, Scanner inputFile, int numValues) {
numValues = inputFile.nextInt();
miles = new double [numValues];
for(int i = 0; i < numValues; i++) {
miles[i] = inputFile.nextDouble();
}
return miles;
} // End readData
// A method used for a program description
public static void progInfo(PrintWriter outputFile, String genInfo) {
genInfo = "This program reads miles from a input file,"
+ "\nstores the values in an array, calculates the"
+ "\nreimbursement and then stores them in a"
+ "\nparalell array.";
outputFile.println(genInfo);
System.out.println(genInfo);
return;
} // End progInfo
// A method for printing out the heading
public static void printHeading(PrintWriter outputFile, String headInfo) {
headInfo = "Mileage" + "\t\t" + "Rimbursements";
outputFile.println(headInfo);
System.out.println(headInfo);
} // End printHeading
// A method for outputing the results of the calcualtions
public static void printTable(PrintWriter outputFile, String outInfo) {
outInfo = "";
} // End printTable
// This method calculates the reimbursement amount
// and stores it in its appropriate array.
public static double [] payCalc(double [] miles, double [] payMe, int numValues) {
for (int i = 0; i < numValues; i++) {
if (miles[i] <= 0) {
payMe[i] = 0.0;
} else if (miles[i] <= 400) {
payMe[i] = miles[i] * 0.18;
} else if (miles[i] >= 400 && miles[i] < 900) {
payMe[i] = 65.00 + 0.15 * (miles[i] - 400);
} else if (miles[i] >= 900 && miles[i] < 1300) {
payMe[i] = 115.00 + 0.12 * (miles[i] - 900);
} else if (miles[i] >= 1300 && miles[i] < 1900) {
payMe[i] = 140.00 + 0.10 * (miles[i] - 1300);
} else if (miles[i] >= 1900 && miles[i] < 2600) {
payMe[i] = 165.00 + 0.08 * (miles[i] - 1900);
} else if (miles[i] >= 2600) {
payMe[i] = 195.00 + 0.06 * (miles[i] - 2600);
} // End if/else
} // End for loop
return payMe;
}
} //End class