提问人:Simon 提问时间:11/4/2023 更新时间:11/6/2023 访问量:94
我的数学逻辑错误在哪里?(爪哇)
Where is the mistake in my math logic? (Java)
问:
主题:为客户提供教育材料的慈善组织需要订购足够的教科书来支持客户的学习需求,并计算购买教科书的总成本。
步骤1:
输入: 教育计划需要支持的学生总数。 每个学生需要的教科书数量。 一本教科书的费用。 迟交教科书请求的预期百分比(此步骤中未使用)。 计算: 根据学生人数计算所需的教科书数量。 计算组织订购教科书的总成本。 输出: 显示汇总报告,包括输入值和计算成本。
步骤2:
输入: 教育计划需要支持的学生总数。 每个学生需要的教科书数量。 一本教科书的费用。 迟交教科书请求的预期百分比。 计算: 计算所需的教科书数量。 计算组织获取教科书并将其交付给客户的总成本。 计算额外费用以考虑延迟的教科书请求。 迟交教科书请求的百分比基于教育计划支持的学生人数,而不是提出请求的客户数量。 输出: 显示输入摘要和成本明细列表。
这是我的代码:
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
然后输出应该是
该订单将支持 231 名学生,每名学生需要 3 本教科书。 所需教科书总数:693 您需要订购 58 本教科书,总费用为 244.76 美元。
考虑到迟到的教科书请求,您应该订购:67 本教科书,费用为 282.74 美元。
但是由于某些原因,我的教科书输出和最终成本是 58 和 244.76
我试图更改或添加更多值以将其四舍五入,但仍然是错误的。
答:
在计算中,您将除以 12,然后四舍五入,这似乎是错误的 - 书籍不是几十本出售的。然后,对于迟到的请求,你乘以迟到的请求乘数,但这不考虑教科书的原始数量()。此外,代码同时使用和 这是多余的。既然你想确保你有足够的教科书,那么只使用更有意义。textbooksToOrder
totalTextbooks
textbooksToOrder
totalTextbooks
Math.round()
Math.ceil()
Math.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();
int totalTextbooks = totalStudents * textbooksPerStudent;
double totalCost = totalTextbooks * costPerTextbook;
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 " + totalTextbooks
+ " textbooks for a total cost of $"
+ String.format("%.2f", totalCost) + ".");
double lateRequestMultiplier = 1.0 + (lateRequestPercentage / 100.0);
double lateRequestTextbooks = totalTextbooks * lateRequestMultiplier;
int roundedLateRequestTextbooks = (int) Math.ceil(lateRequestTextbooks);
double lateRequestCost = roundedLateRequestTextbooks * costPerTextbook;
System.out.println(
String.format("Allowing for late textbook requests, you should " +
"order: %d textbooks for a cost of $%.2f.",
roundedLateRequestTextbooks, lateRequestCost)
);
}
}
这是第 1 步。
import static java.lang.String.valueOf;
import static java.math.RoundingMode.HALF_EVEN;
Scanner in = new Scanner(System.in);
// The total number of students that the educational program will need to support.
// The number of textbooks needed per student.
int a = in.nextInt(), b = in.nextInt();
// The cost of one textbook.
BigDecimal c = in.nextBigDecimal().setScale(2, HALF_EVEN);
// The expected percentage of late textbook requests (not used in this step).
double d = in.nextDouble();
// Calculate the number of textbooks required based on the number of students.
int x = a * b;
// Calculate the total cost to the organization for ordering the textbooks.
BigDecimal y = c.multiply(new BigDecimal(x));
// Display a summary report, including the input values and the calculated cost.
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = $" + c.toPlainString());
System.out.println("d = " + d);
System.out.println("x = " + x);
System.out.println("y = $" + y.toPlainString());
输出
231 3 4.22 .15
a = 231
b = 3
c = $4.22
d = 0.15
x = 693
y = $2924.46
而且,这是第 2 步。
Scanner in = new Scanner(System.in);
// The total number of students that the educational program will need to support.
// The number of textbooks needed per student.
int a = in.nextInt(), b = in.nextInt();
// The cost of one textbook.
BigDecimal c = in.nextBigDecimal().setScale(2, HALF_EVEN);
// The expected percentage of late textbook requests.
double d = in.nextDouble();
// Calculate the number of textbooks required.
int x = a * b;
// Calculate the total cost to the organization of acquiring and delivering the textbooks to its clients.
BigDecimal y = c.multiply(new BigDecimal(x));
// Calculate the additional costs to account for late textbook requests.
// The percentage of late textbook requests is based on the number of students supported by the educational program, not by the number of clients making requests.
BigDecimal z = new BigDecimal(valueOf(x));
z = z.multiply(new BigDecimal(valueOf(d)));
z = z.multiply(c).setScale(2, HALF_EVEN);
// Display a summary of the inputs and an itemized list of the costs.
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = $" + c.toPlainString());
System.out.println("d = " + d);
System.out.println("x = " + x);
System.out.println("y = $" + y.toPlainString());
System.out.println("z = $" + z.toPlainString());
输出
231 3 4.22 .15
a = 231
b = 3
c = $4.22
d = 0.15
x = 693
y = $2924.46
z = $438.67
评论
main(String\[\] args)
)67-58
0.15
15