当我运行5次示威功能时,我一无所获

发布于 2025-01-24 02:25:34 字数 568 浏览 2 评论 0原文

因此,当我运行这个程序时,

#include<iostream>
#include<ctime>

float ex, ey;

class Enemy
{
    public:
        float x, y;
    
        Enemy()
        {
            x = ex;
            y = ey;
        }
    
        void showLocation()
        {
            std::cout<<x<<" , "<<y<<std::endl;
        }
};
int main()
{
    Enemy e;
    for(int i = 0;i<5,i++;)
    {
        ex = rand() % 5 + 1;
        ey = rand() % 5 + 1;
        e.showLocation();
    }
}

我什么都没有,我做错了什么? 我有一个空白的空间,然后说:“流程退出 - 返回代码0”

so when i run this program,

#include<iostream>
#include<ctime>

float ex, ey;

class Enemy
{
    public:
        float x, y;
    
        Enemy()
        {
            x = ex;
            y = ey;
        }
    
        void showLocation()
        {
            std::cout<<x<<" , "<<y<<std::endl;
        }
};
int main()
{
    Enemy e;
    for(int i = 0;i<5,i++;)
    {
        ex = rand() % 5 + 1;
        ey = rand() % 5 + 1;
        e.showLocation();
    }
}

I get nothing, am I doing something wrong?
I get a blank space, then it says, "Process exited - return code 0"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2025-01-31 02:25:34

对象e是在for循环之前初始化的。因此,其数据成员xy的设置等于0.0F(全局变量exey是由默认构造函数隐式初始化的,并且不再更改。

Enemy e;
for(int i = 0;i<5,i++;)
{
    ex = rand() % 5 + 1;
    ey = rand() % 5 + 1;
    e.showLocation();
}

您需要在for循环中声明对象,例如,

for(int i = 0;i<5,i++;)
{
    ex = rand() % 5 + 1;
    ey = rand() % 5 + 1;
    Enemy e;
    e.showLocation();
}

在这种情况下,在for循环的每次迭代中,将创建一个新对象e数据成员将通过其计算值来初始化全局变量exey

The object e is initialized before the for loop. So its data members x and y are set equal to 0.0f (the global variables ex and ey are implicitly zero initialized) by the default constructor and are not changed any more.

Enemy e;
for(int i = 0;i<5,i++;)
{
    ex = rand() % 5 + 1;
    ey = rand() % 5 + 1;
    e.showLocation();
}

You need to declare the object in the for loop for example like

for(int i = 0;i<5,i++;)
{
    ex = rand() % 5 + 1;
    ey = rand() % 5 + 1;
    Enemy e;
    e.showLocation();
}

In this case in each iteration of the for loop there will be created a new object e data members of which will be initialized by calculated values of the global variables ex and ey.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文