我想在cout中添加float平均变量。
完美的方法是什么?

int first , second , third;
cout<<"enter the first number: ";
cin>>first;
cout<<"enter the second number: ";
cin>>second;
cout<<"enter the third number: ";
cin>>third;

cout<<float average=(first+second+third)/3;

最佳答案

你不能那样做。只需在打印之前声明变量。

float average = (first + second + third) / 3;
std::cout << average;


但是,您所能做的就是根本没有变量:

std::cout << (first + second + third)/3;


另请注意,(first+second+third)/3的结果是int,将被截断。如果这不是您的意图,则可能需要将int first, second, third;更改为float first, second, third;

关于c++ - 如何在球场内添加变量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58281720/

10-09 08:56