- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在做一个学校项目,我应该在其中获取各种“FIXME”部分并将它们作为主函数之外的函数的来源。我以为我一切正常,然后没有任何工作。我重新开始,并将范围缩小到导致问题的功能。我将在下面的代码中标记它。也就是说,我可以轻松地将其格式化为函数,而且我的函数语法是正确的答案,但是,如果我在函数中有这些东西而不是没有,那就完全不同了。这是没有函数的代码:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const double pi = 3.14159265;
const double grav = 9.8; // Earth gravity (m/s^2)
// Given time, angle, velocity, and gravity
// Update x and y values
void Trajectory(double t, double a, double v,
double& x, double& y) {
x = v * t * cos(a);
y = v * t * sin(a) - 0.5 * grav * t * t;
return;
}
// convert degree value to radians
double DegToRad(double deg) {
return ((deg * pi) / 180.0);
}
// print time, x, and y values
void PrintUpdate(double t, double x, double y) {
cout << "Time " << fixed << setprecision(0)
<< setw(3) << t << " x = " << setw(3)
<< x << " y = " << setw(3) << y << endl;
return;
}
void PrintIntro() { //This function is going to print the intro to the game!
cout << "Welcome to Upset Fowl!\n";
cout << "The objective is to hit the Mean Swine by launching an Upset Fowl.\n";
}
int main() {
double t = 1.0; // time (s)
double fowlY = 0.0; // object's height above ground (m)
double fowlAngle = 0.0; // angle of launch of fowl (rad)
double fowlVel = 0.0; // velocity of fowl (m/s)
double fowlX = 0.0; // object's horiz. dist. from start (m)
double fowlLandingX = 0.0; // fowl’s horiz. dist. from start (m)
double swineX = 0.0; // distance to swine (m)
double beforeSwineX = 0.0; // distance before swine that is acceptable as a hit (m)
bool didHitSwine = false; // did hit the swine?
srand(time(0));
swineX = 50; //(rand() % 201) + 50; I took out the randomness so I can keep track of answers easily.
PrintIntro();
cout << "\nThe Mean Swine is " << swineX << " meters away.\n";
cout << "Enter fowl launch angle (deg): ";
cin >> fowlAngle;
fowlAngle = ((fowlAngle * pi) / 180.0); // convert to radians
cout << "Enter fowl launch velocity (m/s): ";
cin >> fowlVel;
// FIXME Make into a function called LaunchFaowl
do {
PrintUpdate(t, fowlX, fowlY);
Trajectory(t, fowlAngle, fowlVel, fowlX, fowlY);
t = t + 1.0;
} while (fowlY > 0.0); // while above ground
PrintUpdate(t, fowlX, fowlY);
fowlLandingX = fowlX;
// FIXME Make into a function called DtrmnIfHit
beforeSwineX = swineX - 30;
if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX)) {
cout << "Hit'em!!!" << endl;
didHitSwine = true;
}
else {
cout << "Missed'em..." << endl;
didHitSwine = false;
}
return 0;
}
下面是函数的代码:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const double pi = 3.14159265;
const double grav = 9.8; // Earth gravity (m/s^2)
// Given time, angle, velocity, and gravity
// Update x and y values
void Trajectory(double t, double a, double v,
double& x, double& y) {
x = v * t * cos(a);
y = v * t * sin(a) - 0.5 * grav * t * t;
return;
}
// convert degree value to radians
double DegToRad(double deg) {
return ((deg * pi) / 180.0);
}
// print time, x, and y values
void PrintUpdate(double t, double x, double y) {
cout << "Time " << fixed << setprecision(0)
<< setw(3) << t << " x = " << setw(3)
<< x << " y = " << setw(3) << y << endl;
return;
}
void PrintIntro() { //This function is going to print the intro to the game!
cout << "Welcome to Upset Fowl!\n";
cout << "The objective is to hit the Mean Swine by launching an Upset Fowl.\n";
}
void GetUsrInpt(double piggy, double slope, double Velocity) { // FIXME Make into a function called GetUsrInpt
cout << "\nThe Mean Swine is " << piggy << " meters away.\n";
cout << "Enter fowl launch angle (deg): ";
cin >> slope;
slope = ((slope * pi) / 180.0); // convert to radians
cout << "Enter fowl launch velocity (m/s): ";
cin >> Velocity;
}
int main() {
double t = 1.0; // time (s)
double fowlY = 0.0; // object's height above ground (m)
double fowlAngle = 0.0; // angle of launch of fowl (rad)
double fowlVel = 0.0; // velocity of fowl (m/s)
double fowlX = 0.0; // object's horiz. dist. from start (m)
double fowlLandingX = 0.0; // fowl’s horiz. dist. from start (m)
double swineX = 0.0; // distance to swine (m)
double beforeSwineX = 0.0; // distance before swine that is acceptable as a hit (m)
bool didHitSwine = false; // did hit the swine?
srand(time(0));
swineX = 50; //(rand() % 201) + 50;
PrintIntro();
GetUsrInpt(swineX, fowlAngle, fowlVel);
// FIXME Make into a function called LaunchFaowl
do {
PrintUpdate(t, fowlX, fowlY);
Trajectory(t, fowlAngle, fowlVel, fowlX, fowlY);
t = t + 1.0;
} while (fowlY > 0.0); // while above ground
PrintUpdate(t, fowlX, fowlY);
fowlLandingX = fowlX;
// FIXME Make into a function called DtrmnIfHit
beforeSwineX = swineX - 30;
if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX)) {
cout << "Hit'em!!!" << endl;
didHitSwine = true;
}
else {
cout << "Missed'em..." << endl;
didHitSwine = false;
}
return 0;
}
现在,我要问的是这些程序在哪里误入歧途?我找不到他们这样做的任何理由,而且我已经搜索了所有变量并且认为它们不应该成为问题。我该如何解决?
注意:我不希望人们为我处理所有的 FIXME 部分!我只想知道我哪里出错了,这样我就可以处理其余的问题。
最佳答案
You have set up the function GetUsrInpt to pass the double parameters as >"pass by value" there fore only copies of the values of swineX, >fowlAngle and fowlVel are passed to the function. Changing them in the >function won't change their values in the main function. If you want to >change them and have it known to the main function you have to pass by >reference by using the pointer to the variable. like this
void GetUsrInpt(double* ptrPiggy, double* pSlope, double* pVelocity)
{
//...
cin >> *pPiggy;
cin >> *pSlope;
cin >> *pVelocity;
//...
}
//像这样调用函数:
GetUsrInpt( &swineX, &fowlAngle, &fowlVel );
关于C++ 将内容移动到函数中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33715453/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!