我的问题是,当我编译并运行程序时,小时并没有“小时”列出1,2,3,因为循环继续,并且每行的循环计算也相同。
这就是程序的样子 http://postimg.org/image/htk194eah/
计算错误,小时数假设为1,2 ... 5
我希望它看起来像这样 http://postimg.org/image/pnkvab1j1/
这是我到目前为止所做的:
int main()
{
// Variables
int speed;
int time;
int distance;
// Obtain the speed
cout << "Please input the speed of the vehicle " ;
cin >> speed;
while(speed < 0) // while statement
{
cout << "Please refrain from using a negative number ";
cin >> speed;
}
cout << "Please enter the time, represented in hours, travelled" <<endl;
cin >> time;
// Obtain the time
while(speed < 1)
{
cout<< "Please use a number greater than 1 " <<endl;
cin >> time;
}
// Calculation
distance = speed * time;
cout << endl;
cout << "Hour(s) " << "\t" << "Distance Travelled" << endl;
cout << "____________________________________________" << endl;
// "for" Loop statement
for(int count =1; count <= time; count++)
{
cout << " " << "\t\t" << speed*time << endl;
}
system ("PAUSE");
return 0;
}
答案 0 :(得分:0)
当您在for循环中打印时,速度= 20且时间= 5,因此它始终打印100(在您给出的示例中)。
您希望打印速度*计数(在这种情况下)。
再次打印&#34;&#34;对于小时,这是一个空字符串,您希望打印计数。
cout << count << "\t\t" << speed*count << endl;
答案 1 :(得分:0)
这是正确的程序。你的节目非常好。但是你有一个很小的错误。
#include<iostream.h>
main()
{
// Variables
int speed;
int time;
int distance;
// Obtain the speed
cout << "Please input the speed of the vehicle " ;
cin >> speed;
while(speed < 0) // while statement
{
cout << "Please refrain from using a negative number ";
cin >> speed;
}
cout << "Please enter the time, represented in hours, travelled ";
cin >> time;
// Obtain the time
while(speed < 1)
{
cout<< "Please use a number greater than 1 ";
cin >> time;
}
// Calculation
cout << endl;
cout << "Hour(s) " << "\t" << "Distance Travelled" << endl;
cout << "____________________________________________" << endl;
// "for" Loop statement
for(int count =1; count <= time; count++)
{
cout << count << "\t\t" << speed * count << endl;
}
system ("PAUSE");
return 0;
}