输入第一个数组出了点问题?

时间:2019-03-09 06:33:47

标签: c++

我是C ++的新手,刚学会了创建随机数,因此我创建了一个彩票游戏,该游戏有6个生成的随机数,范围是1到49,用户会猜到它,但是似乎有问题[请参见下面的输出]

]

当我输入第一个数组时,该数组首先在单词(“您的数字是:”)之前输出它

这是我的代码

 /*int lottoNumbers[6];
  int myLotto[6];
  int ok=1;
  int money=100;
  srand(time(0));
    */
  for (int j=0; j<6; j++)
  {
      int n=1+(rand()%49);
      lottoNumbers[j]=n;
  }
  for (int i=0; i<6; i++)
  {
      for (int j=i+1; j<6; j++)
        {
            if (lottoNumbers[i] == lottoNumbers[j])
            {
                lottoNumbers[j]=(rand()%49+2)/2;
            }
        }
  }
  cout<<"Input your Lucky Numbers: "<<endl;
  for (int j=0; j<6; j++)
  {
      cin>>myLotto[j];
  }
  cout<<endl;
  for (int i=1; i<=6; i++)
  {
      for (int j=i+1; j<=6; j++)
      {
          if (myLotto[i]==myLotto[j])
          {
              ok=2;
          }
      }
  }
  for (int i=1; i<=6; i++)
  {
      if (myLotto[i]>49 || myLotto[i]<1 || ok==2)
      {
          cout<<"\nInappropriate. Lost for lottery";
          ok=0;
          break;
      }
  }
  if (ok==1)
  {
      for (int i=0; i<6; i++)
      {
          if (i==1)
          {
              cout<<"\nYour numbers are: "<<endl;
          }
          if (myLotto[i]<=49 && myLotto[i]>=1)
          {
              cout<<myLotto[i]<<" ";
          }
      }
  }
  cout<<endl;
  cout<<"\nWinning number this time: "<<endl;
  for (int i=0; i<6; i++)
  {
      cout<<lottoNumbers[i]<<" ";
  }
  int nr=0;
  for (int i=1; i<=6; i++)
  {
      for (int j=1; j<=6; j++)
      {
          if (myLotto[i]==lottoNumbers[j])
          {
              nr++;
          }
      }
  }
  cout<<endl;
  cout<<endl;

  int jk=0;

  if (jk==0)
  {
      cout<<"You hit "<<nr<<" numbers."<<endl;
      cout<<"You have "<<(nr*100)/6<<"% chances to win."<<endl;
  }else
  {
      cout<<"You GOT THE JACKPOT";
  }
 /* if (nr==1 || nr==2 )
  {
      money = money + 20;
  }
  if (nr==3)
  {
      money = money + 50;
  }
  if (nr==4)
  {
      money = money + 100;
  }
  if (nr==5)
  {
      money = money + 10000;
  }
  if (nr==6)
  {
      money = money + 100000;
  }
  if (nr==0)
  {
      money= money -10;
  }
  cout<<"Your Money: "<<money<<endl;
*/
  cout<<endl;

并且有一种使其更简单的方法

2 个答案:

答案 0 :(得分:0)

在打印循环的时间内,在代码updates[`users/${userUUID}/status`] = statusChange; updates[`users/${userUUID}/email/`] = userEmail; updates[`products/${productUUID}/colour/`] = colour; firebase.database().ref().update(updates); 中,显然i == 1在第一个数字之后被打印。我想您以某种方式忘记了循环从"Your numbers are"开始。

这是一个简单的解决方法,只需更改此

i == 0

对此

 if (ok==1)
  {
      for (int i=0; i<6; i++)
      {
          if (i==1)
          {
              cout<<"\nYour numbers are: "<<endl;
          }
          if (myLotto[i]<=49 && myLotto[i]>=1)
          {
              cout<<myLotto[i]<<" ";
          }
      }
  }

您在代码的多个位置犯了类似的错误,例如

 if (ok==1)
  {
      cout<<"\nYour numbers are: "<<endl;
      for (int i=0; i<6; i++)
      {
          if (myLotto[i]<=49 && myLotto[i]>=1)
          {
              cout<<myLotto[i]<<" ";
          }
      }
  }

应该是这个

for (int i=1; i<=6; i++)
  {
      for (int j=i+1; j<=6; j++)
      {
          if (myLotto[i]==myLotto[j])
          {
              ok=2;
          }
      }
  }

请记住,在C ++中数组从零开始,因此for循环也应该如此。

答案 1 :(得分:0)

注意:我不处理生成的随机数和输入的重复。

我会这样:

#include <iostream>
#include <random>
#include <array>
#include <algorithm>

int main(){
    std::array<int, 6> winningNumbers;

    //generate random numbers
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(1, 50);
    for(auto& i : winningNumbers){
        i = distribution(generator);
    }

    std::array<int, 6> inputNumbers;

    std::cout << "Input your lucky numbers:\n";
    for(auto& i : inputNumbers)
        std::cin >> i;

    std::cout << "\nYour numbers are: ";

    for(const auto& i : inputNumbers)
        std::cout << i << "\n";

    std::cout << "Winning Numbers this time:\n";
    for(const auto& i : winningNumbers){
        std::cout << i << "\n";
    }

    short numbersHit = 0;

    for(const auto& i : winningNumbers){
        if(std::find(inputNumbers.begin(), inputNumbers.end(), i) != inputNumbers.end())
            ++numbersHit;

    }

    std::cout << "You hit " << numbersHit << "\n";
}