gpt4 book ai didi

java - 使用打印到屏幕上的体积的成本差异

转载 作者:行者123 更新时间:2023-12-01 22:26:06 26 4
gpt4 key购买 nike

我正在大学学习计算机编程类(class)的第一年,并且被困在作业的这一部分(老师也到处都是,很难得到澄清)。我们正在制作的这个程序是为了让运输公司确定哪个包裹最便宜。我们采用两个包装的 3 个变量(长x宽x高)并计算体积和成本。但接下来我们应该有关于哪一个更好地使用体积的陈述?这是问题

**程序必须使用数量差异计算成本差异,并给出以下内容(按优先级顺序):

  • 如果没有差异,则显示费用相同
  • 如果其中一个的成本低于另一个的两倍,则显示“略高于”
  • 如果其中一个的成本小于另一个的三倍,则显示为“两倍”
  • 如果其中一个的成本小于另一个的四倍,则显示为“三倍”
  • 如果其中一个的成本小于另一个的五倍,则显示为“四倍”
    否则,显示为计算出的倍数**

我尝试了一些使用卷并比较它们的 if 语句,然后厌倦了成本并将两个包与成本进行比较,但没有得到结果。

    if (package1.calcVolume()==package2.calcVolume()){
System.out.println("The cost of both packages are the same.");
}
else if (Math.max(package1.calcVolume(), package2.calcVolume()) == package1.calcVolume()){

if (package1.calcVolume()<=(2*package2.calcVolume())){

System.out.println("The cost of package 1 is slightly more.");
}
else if (package1.calcVolume()<=(3*package2.calcVolume())){

System.out.println("The cost of package 1 is two times more.");
}
else if (package1.calcVolume()<=(4*package2.calcVolume())){

System.out.println("The cost of package 1 is three times more.");
}
}
else if (Math.max(package1.calcVolume(), package2.calcVolume())== package2.calcVolume()) {

System.out.println("Package 2 is more."); //Place holder for now
}

当包裹 1 输入 2,2,2 作为长x宽x高,包裹 2 输入 1,1,1 时,我们应该得到“第一个包裹是三倍多”。

我没有得到“两次”和“四倍”,并且使用此代码我认为我没有收到任何东西。

最佳答案

每次执行诸如 package1.calcVolume()package2.calcVolume() 之类的操作时,您本质上都是在调用 calcVolume() Package 类中的方法。看起来可能并非如此,但这非常耗时,更不用说一遍又一遍地进行不必要的处理。执行一次:

double p1 = package1.calcVolume();
double p2 = package2.calcVolume();

是的,它应该是 double (或浮点)数据类型,以便正确获取成本的略少部分,因此如果还没有,请更正<的返回类型strong>calcVolume() 方法。现在使用 p1p2 而不是总是调用 calcVolume() 方法。

通过删除所有调用,您最终将消除大量困惑的代码,这将使您的代码更易于阅读和遵循,但最重要的是,您的 CPU 会喜欢您,JVM 也会喜欢您。好吧,也许没那么多,但至少你不需要输入那么多。

哪个包“小于”,哪个包“大于”?套餐 1 是套餐 2 的两倍还是套餐 2 大于套餐 1?这会产生很多 IF/ELSE。一组 IF/Else 就足够了,因此最好将这些名称指定也放入字符串变量中,并允许代码在消息显示到控制台之前确定哪个字符串包含哪个字符串:

String pkgHigh = "Package 'A'";
String pkgLow = "Package 'B'";

现在,哪个包的尺寸(体积)更大:

p1 = 34;
p2 = 79;

由于我们只处理两个包,因此我们假设 p1 的体积将始终等于或大于 p2 的体积,但是嘿,让我们检查一下,但在此之前让我们建立体积差异乘数:

double volDiff = p1 / p2;

此处声明了 volDiff double 型变量,并将其初始化为 p1 和 p2 的除法 (p1/p2)。如果包装“A”体积大于等于包装“B”,则这种划分效果很好。现在让我们看看哪个包的体积实际上更大:

if (p1 != p2 && Math.max(p1, p2) == p2) {
// Package 2 has the greater volume...
volDiff = p2 / p1;
pkgHigh = "Package 'B'";
pkgLow = "Package 'A'";
}

我们可以清楚地看到 p2 大于 p1,因为我们在那里硬编码了值。然而,当代码在现实世界中运行时,硬编码就不会出现这种情况。这些值将根据输入的实际封装尺寸来确定。无论如何,我们知道在这种情况下 p2 大于 p1,下面是上面 IF block 如何确定的:

  • 如果p1不等于p2并且 Math.max()方法确定 p2 是两个包中较大的一个:
  • p2 大于 p1,因此将 p2 除以 p1(p2 ÷ p1)即可获得音量差异乘数;
  • 接下来,将字符串变量 pkgHigh 设置为 “Package 'B'”pkgLow“包装‘A’”。现在,该消息将显示正确的包顺序,该顺序反射(reflect)了较高和较低容量的包。

现在只需编写一系列 IF/Else 条件即可显示请求的结果。整个代码如下所示:

// Double data types should be used for volume values.
double p1 = (double) 32; // Volume of Package 1.
double p2 = (double) 32; // Volume of Package 2.

String msg = ""; // For Message String to Display.
String pkgHigh = "Package 'A'"; // Package designation variable.
String pkgLow = "Package 'B'"; // Package designation variable.

// Double data type should be used for the Volume difference Multiplier.
double volDiff = p1 / p2; // Volume Difference Multiplier.

/* Determine if the variables contents need to change.
The IF block below checks to see if package 1 and
package 2 are not equal and sees if the volume for
package 2 is higher than package one. If both of these
pose as true then the Volume Difference Multiplier is
calculated based on Package 2 divided by Package 1
since Package 2 is deemed to contain a higher volume.
The package designation string variables are also changed
to properly reflect which package is higher or lower
within the message strings. Otherwise package designators
remain the very same as they were initialized to. */
if (p1 != p2 && Math.max(p1, p2) == p2) {
// Package 2 has the greater volume...
volDiff = p2 / p1;
pkgHigh = "Package 'B'";
pkgLow = "Package 'A'";
}

/* Determine the appropriate message to display in console based on the
Volume Difference Multitplier value. Package designation within the
messages has been determined pkg1/pkg2 initialization and within the
above IF block. */

// Packages are the same cost.
if (volDiff == 1.0d) {
msg = "The shipping cost for " + pkgHigh + " and " +
pkgLow + " are the same.";
}
// One Package cost is slightly higher than the other.
else if (volDiff > 1.0d && volDiff < 2.0d) {
msg = "The shipping cost for " + pkgHigh +
" is slightly higher than it is for " +
pkgLow + ".";
}
// One Package cost is Twice as much as the other.
else if (volDiff >= 2.0d && volDiff < 3.0d) {
msg = "The shipping cost for " + pkgHigh +
" is twice the cost than it is for " +
pkgLow + ".";
}
// One Package cost is Triple that of the other.
else if (volDiff >= 3.0d && volDiff < 4.0d) {
msg = "The shipping cost for " + pkgHigh +
" is triple the cost than it is for " +
pkgLow + ".";
}
// One Package cost is Quadruple that of the other.
else if (volDiff >= 4.0d && volDiff < 5.0d) {
msg = "The shipping cost for " + pkgHigh +
" is quadruple the cost than it is for " +
pkgLow + ".";
}
// One Package cost is 'n' times greater than the other.
else {
/* Round DOWN the Multiplier value by using
Math.round() and converting to integer. */
int multiple = Math.round((int) volDiff);
msg = "The shipping cost for " + pkgHigh + " is " +
multiple + " times higher than it is for " +
pkgLow + ".";
}
System.out.println(msg); // Display the info message to Console.

谈论代码困惑。是的,过多的注释也会造成困惑,但都放在代码中以提供帮助。如果需要,可以轻松将其删除。

关于java - 使用打印到屏幕上的体积的成本差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58566552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com