Q:计算每月的电费并获得每月的付款。
我没有正确支付1月,2月,3月等付款。每月应分开付款。 110 = 1月,88 = 2月,依此类推。如果jan小于40,则该值应乘以20。
示例:如果jan = 35,则35 * 20 =等于700。因此,这应适用于所有月份。
int数组1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月。
class test2 {
public static void main(String[] args) {
int months[] = {110, 88, 125,168,210,65,75,98,133,165,175,189};
int x, size, count;
size = months.length;
double rate_chrg=0, payment =0;
count = 0;
x= 0;
while (x<size){
if (months[x] <40){
payment = months[x] * 20;
}
else if(months[x] <60){
payment = months[x] * 30;
}
else if(months[x] <80){
payment = months[x] * 40;
}
else if(months[x] <250){
payment = months[x] * 60;
}
x++;
}
System.out.println("Jan => " + months[0] + " = " +payment);
System.out.println("Feb => " + months[1] + " = "+ payment);
}
}
这是我的输出:
Jan => 110 = 11340.0 Feb => 88 = 11340.0
金额错误,应打印为Jan => 110 = 6600
上面的答案6000是从110 * 60 = 6600
答案 0 :(得分:1)
System.out.println("Jan => " + months[0] + " = " +payment);
System.out.println("Feb => " + months[1] + " = "+ payment);
将这些行放入while
循环中,您会看到不同的地方,问题是payment
将在while
的每个循环之后再次重新初始化
答案 1 :(得分:0)
仅针对一月份的支票
else if(months[x] <250)
是正确的,但是此检查在3月,4月,5月,9月,10月,11月和12月也是正确的,并且因为要遍历整个 months 个数组,所以付款等于最后一个元素从数组中减去250,也就是十二月。所以付款是189 * 60
您需要累计付款或在while循环中打印付款
答案 2 :(得分:0)
int months [] = {110,88,125,168,210,65,75,98,133,165,175,189}; 字符串m [] = {“ Jan”,“ Feb”,“ March”,“ April”,“ May”,“ June”,“ July”,“ Augest”,“ September”,“ October”,“ November”, “十二月”}; 整数x,大小,计数,len; 大小= months.length; len = m.length; 双重付款== 0;
x= 0;
count = 0;
while (x<size && count<= len){
if (months[x] <40){
payment = months[x] * 20;
}
else if(months[x] <60){
payment = months[x] * 30;
}
else if(months[x] <80){
payment = months[x] * 40;
}
else if(months[x] <250){
payment = months[x] * 60;
}
System.out.println(m[count] + ": "+ months[x] + " = " +payment);
x++;
count++;
}
//谢谢,我做到了...:)