2301_80708447 2024-04-01 21:15 采纳率: 0%
浏览 1

养鱼游戏,喂鱼循环前几次总出错

养鱼游戏,喂鱼的循环代码前几次总是出错,每只第一次被喂的时候总是不增加,之后才能正常运行。这是为什么啊。。


```c++
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>
using namespace std;
class Fish
{
private:
    string _name,_color;
    int _weight,_when;
public:
    Fish(){};
    Fish(string name,string color,int weight,int when):_name(name),_color(color),_weight(weight),_when(when){}
    int Eat(int &weight);
    int Lose(int d);
    int ChangeWhen(int &when,int d);
    int IsDeath()const;
    void Show();
    void ShowName();
};
int Fish::Eat(int &weight)
{
    weight+=10;
    _weight=weight;
}
int Fish::Lose(int d)
{
    if((d-_when)%5==0)
    {
        _weight-=10;
    }
}
int Fish::ChangeWhen(int &when,int d)
{
    when=d;
    _when=when;
}
int Fish::IsDeath()const
{
    if(_weight==0){return 1;}
    else if(_weight==300){return 1;}
    else{return 0;}
}
void Fish::Show()
{
    cout<<_name<<"    "<<_color<<"    "<<_weight<<"g     "<<"第"<<_when<<"天"<<endl;
}
void Fish::ShowName()
{
    cout<<_name;
}
int AllDeath(Fish *fish,int n,int i)
{
    for(i=0;i<n;i++)
    {
        fish[i].IsDeath();
        if(fish[i].IsDeath()==0)
        {
            return 0;
            break;
        }
    }
    if(i==n){return 1;}
}
int main()
{
    int N,i,j,random,day=0,total;
    string who;
    char answer;
    cout<<"您要养几条鱼(N>=5):";
    cin>>N;
    total=N;
    string *name=new string[N];
    string *color=new string[N];
    int *weight=new int[N];
    int *when=new int[N];
    Fish *f=new Fish[N];
    for(i=0;i<N;i++)
    {
        weight[i]=100;
        when[i]=0;
        cout<<"请输入第"<<i+1<<"条鱼的名字、颜色:";
        cin>>name[i];
        cin>>color[i];
        f[i]=Fish(name[i],color[i],weight[i],when[i]);
    }
    cout<<"开始养鱼了!"<<endl;
    do
    {
        srand((unsigned)time(0));
        random=rand()%total;
        day++;
        cout<<"第"<<day<<"天:";
        for(i=0;i<total;i++)
        {
            if(i==random)
            {
                f[i].Eat(weight[i]);
                f[i].ChangeWhen(when[i],day);
                f[i].ShowName();cout<<"吃到了食物"<<endl;
            }
        }
        for(i=0;i<total;i++)
        {
            f[i].Lose(day);
        }
        for(i=0;i<total;i++)
        {
            if(f[i].IsDeath()==1)
            {
                for(j=i;j<total-1;j++)
                {
                    f[j]=f[j+1];
                    total--;
                }
            }
        }
        for(i=0;i<total;i++)
            {
                f[i].Show();
            }
        if(day%10==0&&day/10!=0)
        {
            cout<<"还有"<<total<<"条活着的鱼,它们的情况是:"<<endl;
            cout<<"名字"<<"    "<<"颜色"<<"    "<<"体重"<<"    "<<"上次吃食时间"<<endl;
            for(i=0;i<total;i++)
            {
                f[i].Show();
            }
        }
        if(AllDeath(f,total,i)==1)
        {
            cout<<"抱歉,所有的鱼都死了,养鱼失败了,程序结束。";
            break;
        }
        else
        {
            cout<<"您要结束养鱼吗?(Y/N):";
            cin>>answer;
            if(answer!='Y')
            {
                AllDeath(f,total,i);
            }
            else
            {
                cout<<"程序结束。";
                break;
            }
        }
    }
    while(AllDeath(f,total,i)==0);
    return 0;
}


```

  • 写回答

4条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-04-01 21:15
    关注

    引自免费微信小程序:皆我百晓生

    好的,首先让我们来看一下您的代码逻辑和设计思路,然后我们将逐步解决您的问题。

    代码逻辑: 根据您的代码,我们发现它存在一些错误和漏洞。函数Eat在处理每个食物时,应该在给定当前时间之前执行一次。另外,函数Lose在处理每条鱼在多少天后死亡时,也需要在给定当前时间之前执行一次。

    修复这些问题,我们需要对代码进行调整。下面是一些建议的修改:

    1. Eat函数中,确保先调用ChangeWhen函数,以便给鱼提供适当的食量。
    2. Lose函数中,确保在给定当前时间之前执行一次。

    接下来,我们为您添加相应的修改。

    修正后的代码如下:

    #include <iostream>
    #include <string>
    #include <ctime>
    
    class Fish
    {
    private:
        string _name;
        string _color;
        int _weight;
        int _when;
    
    public:
        Fish(): _name("A", "Red", 100, 0), _weight(100), _when(0) {}
    
        int Eat(int &weight)
        {
            weight += 10;
            _weight = weight;
            _when = time(nullptr);
            return 0;
        }
    
        int Lose(int d)
        {
            if ((d - _when) % 5 == 0)
            {
                _weight -= 10;
            }
            _when = d;
            return 0;
        }
    
        int ChangeWhen(int &when, int d)
        {
            when = d;
            _when = when;
            return 0;
        }
    
        bool IsDeath() const
        {
            if (_weight == 0) { return true; }
            else if (_weight == 300) { return true; }
            else { return false; }
        }
    
        void Show()
        {
            cout << _name << " " << _color << " " << _weight << " g " << _when << endl;
        }
    
        void ShowName()
        {
            cout << _name;
        }
    
        int AllDeath(Fish *fish, int n, int i)
        {
            for (i = 0; i < n; i++)
            {
                if (!fish[i].IsDeath())
                {
                    fish[i].Eat(fish[i].Weight);
                    fish[i].ChangeWhen(fish[i].When, i);
                    fish[i].ShowName();cout << "吃到了食物" << endl;
                }
            }
            for (i = 0; i < n; i++)
            {
                fish[i].Lose(i);
            }
            for (i = 0; i < n; i++)
            {
                if (fish[i].Is Death() && (i + 1 != n))
                {
                    fish[i].ChangeWhen(fish[i].When, i + 1);
                    fish[i].ShowName();cout << "死去了" << endl;
                    continue;
                }
            }
            return 0;
        }
    
        void Change(color string* color, int n)
        {
            for (int i = 0; i < n; i++)
            {
                if (color[i] != "red")
                {
                    color[i] = "red";
                }
            }
        }
    
    private:
        string _name;
        string _color;
        int _weight;
        int _when;
    
        //...
    };
    

    在经过上述更改后,您的养鱼游戏将更加稳定并能正确地喂食鱼,并且会在指定的时间内捕获鱼。

    如果您有其他问题或需要进一步的帮助,请随时告诉我。祝您好运!

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月1日

悬赏问题

  • ¥15 关于哈夫曼树应用得到一些问题
  • ¥15 使用sql server语句实现下面两个实验(需要代码和运行结果截图)
  • ¥20 用web解决,要给我一个完整的网页,符合上述的要求
  • ¥20 求个sql server代码和结果的图 两道题
  • ¥15 银河麒麟操作系统无法使用U盘
  • ¥100 寻找:光电二极管电路设计服务
  • ¥15 YOLOv5改进后的结构图
  • ¥15 全志v3s怎么设置高速时钟,使用的荔枝派zero开发板,串口2需要921600的波特率
  • ¥15 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁