|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Chiller 于 2021-10-31 14:36 编辑
以下为原代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
float price;
int time=0;
cout<<"输入话费";
cin>>price;
if(price<=0.5)
{
time=3;
} //前面部分未出错
else
{
price=price-0.5;
cout<<price/0.2<<endl;
time=price/0.2;
cout<<time<<endl;
time=time+3;
}
cout<<time<<endl;
}
当我输入1.3时,time本该为7,输出时却是6;price/0.2输出的结果是4,可下面time被赋值后输出3,导致结果为6
请问这是为什么?
- price=1.3>0.5所以:
- else
- {
- price=price-0.5;//price=0.7
- cout<<price/0.2<<endl;
- time=price/0.2;//time=int(3.5)=3
- cout<<time<<endl;
- time=time+3;//time=3+3=6
- }
复制代码
|
|