在这项任务中,我将根据行驶里程计算报销价值。如果里程值小于或等于0,我应该输出里程值和“*****”来代替报销价值。
我遇到的麻烦就是让它打印出5颗星。我可以让它打印星星,但后面跟着一个“0”,我需要它来打印星星。
import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
public class Asgn6 {
static Toolkit tools = new Toolkit();
public static void main(String[] args) throws IOException {
// Declare variables and define file names
double mileage = 0.0; // Amount of mileage traveled
double reimbursement = 0.0; // Amount of reimbursement for mileage
int numValues = 0; // Number of mileage values in input file
final String INPUT_FILE = ("Input.txt");
final String OUTPUT_FILE = ("Output.txt");
String heading1 = "Mileage Traveled";
String heading2 = "Reimbursement";
// Access the input/output files
File inputDataFile = new File(INPUT_FILE);
Scanner inputFile = new Scanner(inputDataFile);
FileWriter outputDataFile = new FileWriter(OUTPUT_FILE);
PrintWriter outputFile = new PrintWriter(outputDataFile);
System.out.println("Reading file " + INPUT_FILE + "\r\n" +
"Creating file " + OUTPUT_FILE);
// Display the results
printHeading(heading1, heading2, outputFile);
// Read first number of file to determine number of values in file
numValues = inputFile.nextInt();
// Read input file and calculate reimbursement
while (inputFile.hasNext()) {
mileage = inputFile.nextDouble();
if (mileage <= 0) {
reimbursement = 0.0;
}
else if (mileage < 400) {
reimbursement = 0.18 * mileage;
}
else if (mileage < 900) {
reimbursement = 65 + 0.15 * (mileage - 400);
}
else if (mileage < 1300) {
reimbursement = 115 + 0.12 * (mileage - 900);
}
else if (mileage < 1900) {
reimbursement = 140 + 0.10 * (mileage - 1300);
}
else if (mileage < 2600) {
reimbursement = 165 + 0.08 * (mileage - 1900);
}
else {
reimbursement = 195 + 0.06 * (mileage - 2600);
}
summaryOutput(mileage, reimbursement, outputFile);
} // end while loop
inputFile.close(); // close the input file
outputFile.close(); // close the output file
System.exit(0);
} // End main
// *********************************************************************
// Methods Section
// Display output heading
public static void printHeading(String heading1, String heading2,
PrintWriter outputFile) {
System.out.println();
System.out.printf("%-10s\t %s %n", heading1, heading2);
outputFile.printf("%-10s\t %s %n", heading1, heading2);
}// End printHeading
//**********************************************************************
// Display results summary
public static void summaryOutput(double mileage, double reimbursement,
PrintWriter outputFile) {
String noMileage = "*****"; // String to display stars for no
// reimbursement value
if (reimbursement > 0) {
System.out.println(tools.leftPad(mileage, 8, "0.0") +
tools.leftPad(reimbursement, 25, "0.00"));
outputFile.println(tools.leftPad(mileage, 8, "0.0") +
tools.leftPad(reimbursement, 25, "0.00"));
} // End if
else {
System.out.println(tools.leftPad(mileage, 8, "0.0") +
tools.leftPad(reimbursement, 25, noMileage));
outputFile.println(tools.leftPad(mileage, 8, "0.0") +
tools.leftPad(reimbursement, 25, noMileage));
} // End else
} // End summaryOutput
} // End class
该程序运行正常。我只是想不出5星的字符串。
提前感谢您的帮助!
答案 0 :(得分:0)
你想使用关键字continue
,它只是跳到while循环体的末尾,只是继续代码,所以你的代码应该是这样的:
if (mileage <= 0) {
System.out.println("*****");
continue;
}