问题描述
某些保险公司会根据驾驶员获得驾驶执照的年限和过去五年来提出的理赔次数来提供汽车保险费的折扣。 在此程序中,用户输入(以整数形式) •years驾驶执照的年数 •索赔最近五年的保险索赔数量 最初将计算的“标准溢价”的(整数)百分比计算如下: •如果年份 150,则拒绝保险 •否则,如果百分比在100%和150%之间,则将百分比设置为100 •(否则,百分比不会调整)
cout << "enter years licenced: " << endl;
cin >> years;
cout << "enter number of claims: " << endl;
cin >> claims;
if (years < 5)
{
percentage = 100 - (10 * years) + (20 * claims);
cout << "percentage from 0 to 5:" << percentage << endl;
}
else (years > 5);
{
percentage = 50 + (20 * claims);
cout << "percentage higher than 5:" << percentage << endl;
}
if (percentage > 150)
{
cout << "insurance is refused." << percentage << endl;
}
else if (100 <= percentage <= 150)
{
cout << "percentage = 100." << endl;
}
else;
{
cout << "insurance is refused." << endl;
}
return 0;
解决方法
-
替换
else if (100 <= percentage <= 150)
使用
else if (100 <= percentage && percentage <= 150)
在C ++中,比较运算符的链接与数学中的链接不同。要获得数学含义,请不要使用链接,而应将单个比较与逻辑和(
&&
)连接起来。 -
替换
else (years > 5);
使用
else if (years > 5)
;
等效于{}
,因此它使else
分支为空,下面的代码(多行{...}
)将无条件执行。 -
替换
else;
使用
else