我在这里有一个任务:用PayPerView ()方法定义一个类,用于计算租来的电影的每月费用。该方法应采用一个表示租用电影数量的参数,并将费用作为双倍返回。
租费为每部电影6.99美元。每月租金超过10英镑的优先客户可获得5%的折扣。
到目前为止,这是我的代码:
public class PayPerView {
public static void main(String [] args){
}
public static double moviefee(int n){
double fee = 6.99;
double total= n*fee;
System.out.println("Your balance due this month is" + total);
//return 6.99*n * ((n>10) ? 0.95:1);
}}我知道这很糟糕,很抱歉,您可以忽略我注释掉的最后一行代码,因为我要重做它,并将它转换为if语句。我想也许我应该用数组,但我不能用?因为我不知道要租多少部电影?租多少部电影,我应该用一张列队表吗?
发布于 2013-12-11 01:08:23
我不明白如果您没有任何数据,为什么要使用ArrayList。
你可能会想尝试一些这样的方法:
double total = n * fee;
if (n > 10) {
total *= 0.95;
}
我还看到您希望使用三元操作符,因此可以用以下代码块替换上面的代码块
double total = n * fee * (n > 10 ? 0.95 : 1.0);发布于 2013-12-11 01:07:35
实际上,你评论掉的那句话看上去和你想要做的差不多。有什么特别的地方吗?
如果你真的需要在控制台上输出结果.
final double fee = 6.99;
double total = n * fee * (n > 10 ? .95 : 1.0);
System.out.println("Your balance due this month is" + total);
return total;发布于 2013-12-11 01:09:02
你在正确的轨道上
public static double moviefee(int n){
double fee = 6.99;
double total;
if(n <= 10){
total= n*fee;
}
else{
total= n*fee - (n*fee*0.05); //5 percent discount
}
System.out.println("Your balance due this month is" + total);
return total;
}编辑:添加double total;
https://stackoverflow.com/questions/20508607
复制相似问题