坚持使用 if 语句编写一个简单的方法
Stuck on writing a simple method with an if statment
您好,基本上我需要编写一种方法来计算衬衫的价格。订购的前 10 件衬衫按每件 20 美元收费,之后的任何衬衫按每件 15 美元收费。有没有人能帮我解决这个问题,这就是我目前所拥有的,谢谢。
public static double calculateCost(int ShirtsOrdered) {
double cost = 0.0;
if (ShirtsOrdered <= 10) {
cost = cost + 20.00 * ((ShirtsOrdered) / 1);
} else if ((ShirtsOrdered > 10) {
cost = cost + 15.00 * ((ShirtsOrdered) / 1);
return cost;
}
你必须数数 10 之后有多少件衬衫
public static double calculateCost(int ShirtsOrdered) {
double cost = 0.0;
if (ShirtsOrdered <= 10) {
cost = cost + 20.00 * ((ShirtsOrdered) / 1);
} else if ((ShirtsOrdered > 10) {
cost = cost + 20.00 * (10 / 1);//<-- here the cost for 10 first shirt
cost = cost + 15.00 * ((ShirtsOrdered-10) / 1); //<-- here make the count
return cost;
}
最短的解决方案是:
public static double calculateCost(int ShirtsOrdered) {
if (ShirtsOrdered > 10){
return 200.0 + (ShirtsOrdered - 10) * 15.0;
}
return ShirtsOrdered * 20.0;
}
您好,基本上我需要编写一种方法来计算衬衫的价格。订购的前 10 件衬衫按每件 20 美元收费,之后的任何衬衫按每件 15 美元收费。有没有人能帮我解决这个问题,这就是我目前所拥有的,谢谢。
public static double calculateCost(int ShirtsOrdered) {
double cost = 0.0;
if (ShirtsOrdered <= 10) {
cost = cost + 20.00 * ((ShirtsOrdered) / 1);
} else if ((ShirtsOrdered > 10) {
cost = cost + 15.00 * ((ShirtsOrdered) / 1);
return cost;
}
你必须数数 10 之后有多少件衬衫
public static double calculateCost(int ShirtsOrdered) {
double cost = 0.0;
if (ShirtsOrdered <= 10) {
cost = cost + 20.00 * ((ShirtsOrdered) / 1);
} else if ((ShirtsOrdered > 10) {
cost = cost + 20.00 * (10 / 1);//<-- here the cost for 10 first shirt
cost = cost + 15.00 * ((ShirtsOrdered-10) / 1); //<-- here make the count
return cost;
}
最短的解决方案是:
public static double calculateCost(int ShirtsOrdered) {
if (ShirtsOrdered > 10){
return 200.0 + (ShirtsOrdered - 10) * 15.0;
}
return ShirtsOrdered * 20.0;
}