Java代码数学逻辑错误排查:教科书订购计算结果不符求助
问题背景
某教育材料慈善机构需订购教科书满足学员需求并计算采购总成本,流程分为两步:
- 步骤1:输入学员总数、每人所需教科书数量、单本成本、延迟请求百分比(本步骤未使用),计算所需教科书总数及订购总成本并输出汇总报告;
- 步骤2:使用相同参数,计算含延迟请求的订购量、总成本及额外成本并输出明细。
现有Java代码
import java.util.Scanner; public class ProgramSummary { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); // Input int totalStudents = scnr.nextInt(); // Total number of students int textbooksPerStudent = scnr.nextInt(); // Number of textbooks needed per student double costPerTextbook = scnr.nextDouble(); // Cost of one textbook double lateRequestPercentage = scnr.nextDouble(); // Expected percentage of late textbook requests // Calculate number of textbooks required int totalTextbooks = totalStudents * textbooksPerStudent; int textbooksToOrder = (int) Math.ceil((double) totalTextbooks / 12); // Calculate total cost double totalCost = textbooksToOrder * costPerTextbook; // Output System.out.println("Total number of students:"); System.out.println("Number of textbooks needed per student:"); System.out.println("Cost per textbook:"); System.out.println(); System.out.println("This order will support " + totalStudents + " students, each needing " + textbooksPerStudent + " textbooks."); System.out.println("Total textbooks needed: " + totalTextbooks); System.out.println("You will need to order " + textbooksToOrder + " textbooks for a total cost of $" + String.format("%.2f", totalCost) + "."); // Input for late requests System.out.println(); System.out.println("Please input the expected percentage of late textbook requests:"); System.out.println(); double lateRequestMultiplier = 1.0 + (lateRequestPercentage / 100.0); double lateRequestTextbooks = (textbooksToOrder * lateRequestMultiplier); int roundedLateRequestTextbooks = (int) Math.round(lateRequestTextbooks); double lateRequestCost = (double) roundedLateRequestTextbooks * costPerTextbook; // Modified output to match expected format System.out.print("Allowing for late textbook requests, you should order: " + (int) Math.ceil(roundedLateRequestTextbooks) + " textbooks for a cost of $" + String.format("%.2f", lateRequestCost) + "."); } }
问题现象
输入参数231、3、4.22、0.15时,预期最后输出为:
Allowing for late textbook requests, you should order: 67 textbooks for a cost of $282.74.
但实际输出为58本和244.76美元,调整后仍未解决,需排查数学逻辑错误。
错误排查与修正
核心错误点
延迟百分比解析逻辑错误
代码中lateRequestMultiplier = 1.0 + (lateRequestPercentage / 100.0)将输入的0.15解析为0.15%,导致乘数仅为1.0015,无法得到预期的15%增量。结合预期结果,输入的0.15实际代表15%,应直接使用1.0 + lateRequestPercentage作为乘数。冗余的二次输入提示
步骤2中打印的Please input the expected percentage of late textbook requests:属于多余逻辑,需求明确要求使用同一组输入参数,该提示会导致程序等待额外输入,与需求不符。无意义的整数ceil操作
(int) Math.ceil(roundedLateRequestTextbooks)中,roundedLateRequestTextbooks已经是Math.round处理后的整数,对整数执行ceil操作没有实际作用,直接使用该整数即可。
修正后的代码
import java.util.Scanner; public class ProgramSummary { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); // 输入参数 int totalStudents = scnr.nextInt(); int textbooksPerStudent = scnr.nextInt(); double costPerTextbook = scnr.nextDouble(); double lateRequestPercentage = scnr.nextDouble(); // 步骤1:计算基础订购量与总成本 int totalTextbooks = totalStudents * textbooksPerStudent; int textbooksToOrder = (int) Math.ceil((double) totalTextbooks / 12); double totalCost = textbooksToOrder * costPerTextbook; // 步骤1输出 System.out.println("Total number of students: " + totalStudents); System.out.println("Number of textbooks needed per student: " + textbooksPerStudent); System.out.println("Cost per textbook: $" + String.format("%.2f", costPerTextbook)); System.out.println(); System.out.println("This order will support " + totalStudents + " students, each needing " + textbooksPerStudent + " textbooks."); System.out.println("Total textbooks needed: " + totalTextbooks); System.out.println("You will need to order " + textbooksToOrder + " textbooks for a total cost of $" + String.format("%.2f", totalCost) + "."); // 步骤2:计算含延迟请求的订购量与成本 System.out.println(); // 修正延迟乘数逻辑:输入0.15代表15%的增量 double lateRequestMultiplier = 1.0 + lateRequestPercentage; double lateRequestTextbooks = textbooksToOrder * lateRequestMultiplier; // 向上取整确保满足所有需求 int roundedLateRequestTextbooks = (int) Math.ceil(lateRequestTextbooks); double lateRequestCost = roundedLateRequestTextbooks * costPerTextbook; // 步骤2输出 System.out.print("Allowing for late textbook requests, you should order: " + roundedLateRequestTextbooks + " textbooks for a cost of $" + String.format("%.2f", lateRequestCost) + "."); scnr.close(); } }
验证结果
输入231 3 4.22 0.15后,计算过程如下:
- 总需求书本数:
231×3=693 - 基础订购量(按12本/箱向上取整):
ceil(693/12)=58 - 延迟增量乘数:
1+0.15=1.15 - 含延迟的订购量:
ceil(58×1.15)=ceil(66.7)=67 - 总成本:
67×4.22=282.74
最终输出与预期完全一致。
内容的提问来源于stack exchange,提问作者Simon

