在开始之前,我想说的是,在编写代码时,我非常喜欢初学者,所以如果我提出的问题看起来非常基本,我会道歉。< / p>
说到这一点,我在我的代码中挣扎着返回方法。我能够编写这个程序,而不是像我一样将它分成方法,但我被告知在编码时最好将它分解为方法,以便调试更容易。
以下代码似乎有一些主要缺陷。
public static boolean hybridNot()
{
String typeCar = input("Hybrid or Electric?");
boolean electric = false;
if (typeCar.equalsIgnoreCase("Electric"))
{
electric = true;
}
else if (typeCar.equalsIgnoreCase("Hybrid"))
{
electric = false;
}
else
{
print("Sorry I didn't understand that");
}
return;
}
public static boolean solarNot()
{
String panelsMaybe = input("Solar panel?");
boolean solarPanel = false;
if (panelsMaybe.equalsIgnoreCase("Yes"))
{
solarPanel = true;
}
else if (panelsMaybe.equalsIgnoreCase("No"))
{
solarPanel = false;
}
else
{
print("Sorry I didn't understand that");
}
return;
}
public static int discountNot()
{
final int basicPrice = 20000;
final int electricCost = 2000;
final int solarCost = 5000;
boolean electric = hybridNot();
boolean solarPanel = solarNot();
int totalPrice;
if ((solarPanel = true) || (electric = true))
{
totalPrice = basicPrice + solarCost + electricCost - 500;
}
else if ((solarPanel = true) || (electric = false))
{
totalPrice = basicPrice + solarCost;
}
else if ((solarPanel = false) || (electric = true))
{
totalPrice = basicPrice + electricCost;
}
else
{
totalPrice = basicPrice;
}
return;
}
public static void totalCost()
{
final int basicPrice = 20000;
final int electricCost = 2000;
final int solarCost = 5000;
final int discountCost = 500;
boolean hybrid = hybridNot();
boolean solarPanel = solarNot();
int finalPrice = 0;
finalPrice = discountNot();
if (finalPrice >= 26500)
{
print("Basic Price: " + basicPrice + "\n" + "Electric model: " + electricCost + "\n" + "Solar Panel: " + solarCost + "\n" + "Discount: " + discountCost);
}
else if (finalPrice >= 25000)
{
print("Basic Price: " + basicPrice + "\n" + "Solar Panel: " + solarCost);
}
else if (finalPrice >= 22000)
{
print("Basic Price: " + basicPrice + "\n" + "Electric model: " + electricCost);
}
else
{
print("Basic Price: " + basicPrice);
}
print("Total: " + finalPrice);
}
出于某种原因,在继续下一个方法之前,方法hybridNot和solarNot似乎重复了一遍。对我而言,似乎我可能对我在方法结束时返回的内容有疑问,但老实说,我无法弄清楚错误是什么。方法totalCost似乎忽略了方法discountNot中的if语句,并且布尔值没有正确传递给totalCost,我只得到finalPrice >= 26500
时的值。
同样,我一般都是Java的新手,而且我也是stackoverflow的新手(所以嗨!!)所以请告诉我,如果我做错了什么,我会做下次正确看看!谢谢!!
答案 0 :(得分:2)
您应该根据方法返回类型返回值。
public static boolean hybridNot()
{
String typeCar = input("Hybrid or Electric?");
return (typeCar.equalsIgnoreCase("Electric"))
}
我不是在谈论在检查状态的函数中获取输入 - 它本身就是邪恶的。
现在这里:
if ((solarPanel = true) || (electric = true))
你做作业,而不是比较。比较你可以这样做:
if (solarPanel || electric)
因为变量已经是布尔值。
下面:
boolean hybrid = hybridNot();
boolean solarPanel = solarNot();
int finalPrice = 0;
finalPrice = discountNot();
discountNot
会再次致电hybridNot
和solarNot
:
public static int discountNot()
{
...
boolean electric = hybridNot();
boolean solarPanel = solarNot();
答案 1 :(得分:1)
您没有在函数中返回任何值。使用:
public static boolean hybridNot(){
//...
return electric;
}
返回boolean
值electric
。在其他函数中使用类似的语法来返回相关变量。
此外,方法重复的原因是因为你打电话给他们两次:
public static int discountNot()
{
//...
boolean electric = hybridNot(); //here
boolean solarPanel = solarNot();
//...
}
public static void totalCost()
{
//...
boolean hybrid = hybridNot(); //and here
boolean solarPanel = solarNot();
//...
}