猿问

想用C++编写一个农夫过河程序,不知如何改变类中数组的值,求指教

# include"iostream"

using namespace std;

class Game

{

public:

 int m[4];

 int n;

 void bijiao(int m[4])

 {

  if(m[0]==0)      //人不在

  {

   if(m[1]==0)  //狼不在

   {

    if(m[2]==1)     //羊在

    {

     if(m[3]==1)    //菜在

      {

       cout<<"很遗憾,羊吃了菜"<<endl;

       n=0;

          }

    }

   }

   else       //狼在

   {

    if(m[2]==1)    //羊在

    {

     cout<<"很遗憾,狼吃掉了羊"<<endl;

     n=0;

    }

   }

  }

 }

};

class Cout

{

public:

 int c[4],d[4];

 void shuchu(int c[4],int d[4])

 {

  cout<<"                              |               |             "<<endl;

  cout<<"                              |               |             "<<endl;

  cout<<"                              |               |             "<<endl;

  cout<<"                              |               |             "<<endl;

  cout<<"            ";

  if(c[0]==0) cout<<"    ";else cout<<"狼  ";if(c[1]==0) cout<<"    ";else cout<<"羊  ";

  if(c[2]==0) cout<<"    ";else cout<<"菜  ";if(c[3]==0) cout<<"      ";else cout<<"农夫  ";

  cout<<"|               |";

  if(d[0]==0) cout<<"    ";else cout<<"农夫  ";if(d[1]==0) cout<<"    ";else cout<<"菜  ";

  if(d[2]==0) cout<<"    ";else cout<<"羊  ";if(d[3]==0) cout<<"    ";else cout<<"狼"<<endl;

     cout<<"                              |               |             "<<endl;

  cout<<"                              |               |             "<<endl;

  cout<<"                              |               |             "<<endl;

  cout<<"                              |               |             "<<endl;

 }

 void tishi()

 {

  cout<<"请选择农夫带着谁过河,输入代表的数字,摁Enter确认"<<endl;

  cout<<"1.菜"<<endl<<"2.羊"<<endl<<"3.狼"<<endl;

 }

};

class choice:public Cout

{

public:

 void xuanze1()
//感觉问题出现在这里,switch语句执行后,并不能够改变数组C的值
 {

  int i;

  cin>>i;

  switch(i)

  {

  case '1':c[2]=1,c[3]=1,d[0]=0,d[1]=0;

  case '2':c[1]=1,c[3]=1,d[0]=0,d[2]=0;

  case '3':c[0]=1,c[3]=1,d[0]=0,d[3]=0;

  }

 }

 void xuanze2()

 {

  int j;

  cin>>j;

  switch(j)

  {

  case '1':c[2]=0,c[3]=0,d[0]=1,d[1]=1;

  case '2':c[1]=0,c[3]=0,d[0]=1,d[2]=1;

  case '3':c[0]=0,c[3]=0,d[0]=1,d[3]=1;

  }

 }

};
# include "iostream"

using namespace std;

# include "game.h"

void main()

{

 Game a;

 for(int i = 0; i < 5; i++)

  {

    a.m[i] =0;

  }

 Game b;

 for(int i = 0; i < 5; i++)

  {

    b.m[i] =1;

  }

 Cout q;

 choice o;

 while(b.m[0]==1||b.m[1]==1||b.m[2]==1||b.m[3]==1)

 {

 q.shuchu(a.m,b.m);

 q.tishi();

 o.xuanze1();

 a.bijiao(a.m);

 b.bijiao(b.m);

 system("cls");

 q.shuchu(a.m,b.m);

 q.tishi();

 o.xuanze2();

 a.bijiao(a.m);

 b.bijiao(b.m);

 system("cls");

 }

 system("pause");

};

 问题存在于执行switch语句后,类Cout中的C D数组值,并不能够改变,求教如何实现这个程序,多谢了

刘冬ld
浏览 1401回答 1
1回答

你小子可以啊

你的if条件语句怎么套了这么多。。。看着都麻烦
随时随地看视频慕课网APP
我要回答