在进行编码作业时,我们必须合并方法并返回它。然而,这不是重点。我在价格计算部分遇到困难。好吧,这就是我所坚持的要点。当程序询问时,您的汽车是进口的吗?如果答案是肯定的,您将被征收 7% 的进口税,如果答案是否定的,则该费用被免除。现在,该程序要求提供四项服务,根据是或否,将根据他们的回答添加费用。
因此,如果用户想要换油和调整,并且如果他们的汽车是进口汽车,则服务价格将与进口税一起添加,或者如果汽车不是进口汽车但想要所有服务,则费用将显示,而不会添加的进口税等...最终结果将显示“税前,它将是 $#,税后,您的总计是...”需要一个 if 语句,但我不知道从哪里开始,因为我是主要是在“是”或“否”上挣扎......有什么帮助吗?我的教授建议我使用累加器,所以我添加了一个。没有用,我迷路了,任何帮助将不胜感激。
import java.util.Scanner;
public class CarMaintenance
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String car;
//capture car
System.out.println("What is the make of your car");
car = keyboard.nextLine();
String answer;
boolean yn;
System.out.println("Is your car an import?");
while (true)
{
answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("yes"))
{
yn = true;
break;
}
else if (answer.equalsIgnoreCase("no"))
{
yn = false;
break;
}
else
{
System.out.println("Sorry, I didn't catch that. Please answer yes or no");
}
}
String[] services = {"Oil Change", "Coolant Flush", "Brake Job", "Tune Up"};
for(int i=0; i<services.length; i++)
{
System.out.println("Do you want a " +services[i]);
answer = keyboard.next();
}
double amount = 0;
double[] price = {39.99, 59.99, 119.99, 109.99};
for(int i =0; i<price.length; i++)
{
amount = (price[i] + price [i]);
}
// double total = 0;
double c = 0;
c = car(amount);
// c = car(total);
System.out.println("The price for your services for your" + " " + car + " " + "is" + " "+ c + ".");
}
public static double car(double amount)
{
return (double) ((amount-32)* 5/9);
}
}
在讨论您的代码之前,我建议您阅读这篇文章。
虽然有点轻率,但是却有深刻的道理。它与我在 40 年前学到的调试技术完美对应。
首先我提请您注意这一点:
System.out.println("Is your car an import?");
while (true)
{
answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("yes"))
{
yn = true;
break;
}
else if (answer.equalsIgnoreCase("no"))
{
yn = false;
break;
}
else
{
System.out.println("Sorry, I didn't catch that. Please answer yes or no");
}
}
该代码在提出“是或否”问题并获得答案方面做得非常好。它包含在用户响应无法识别为“是”或“否”时重试的代码。
所以我猜测有人为你编写了该代码......或者你在某个地方找到了它。您需要仔细阅读该代码,并确保您准确理解它是如何工作的。
接下来我提请大家注意:
for(int i=0; i<services.length; i++)
{
System.out.println("Do you want a " +services[i]);
answer = keyboard.next();
}
这段代码没有意义。 (向你的橡皮鸭解释一下!)
您之前的代码是如何提出是/否问题的一个很好的示例。但事实并非如此:
然后这个:
double amount = 0;
double[] price = {39.99, 59.99, 119.99, 109.99};
for(int i =0; i<price.length; i++)
{
amount = (price[i] + price [i]);
}
看起来这是试图将服务项目的价格添加到总金额中。但是:
如何解决这个问题?
有些事情从我上面所说的应该是显而易见的。
记住第一个
for
循环中的答案并在第二个for
循环中使用它们的问题......实际上是一个你可以/应该避免的问题。相反,请将两个循环合并为一个。这是一些伪代码:
for each service:
ask the user if he wants this service
if the user wants this service:
add the service cost to the total.
理解其中的逻辑,并将其翻译成Java代码。 (是的,我可以为你写,但这违背了目的!)
如果您希望将费用添加到答案的价值中,您应该仅在一个 for 循环中执行此操作,因为服务和价格的索引一致,只需使用 和 if 语句来检查响应是否为“是”
String[] services = {"Oil Change", "Coolant Flush", "Brake Job", "Tune Up"};
for(int i=0; i<services.length; i++)
{
System.out.println("Do you want a " +services[i]);
answer = keyboard.next();
if(answer.equalsIgnoresCase()){
answer += prices[i]
}
}
double amount = 0;
double[] price = {39.99, 59.99, 119.99, 109.99};
for(int i =0; i<price.length; i++)
{
amount = (price[i] + price [i]);
}