简体   繁体   English

C++ 中的 If 语句具有多个变量

[英]If statement in C++ with more than one variable

I started trying to code in C++ and I made a very simple game.我开始尝试在 C++ 中编写代码,并制作了一个非常简单的游戏。 As the game has more than one viable solution, I tried to make an if with more than one variable, but it doesn't seem to work as it doesn't matter the number I type, it always says I'm correct.由于游戏有不止一个可行的解决方案,我尝试用多个变量创建一个 if,但它似乎不起作用,因为我输入的数字并不重要,它总是说我是正确的。

int main() {
    string Username;
    cout << "Type your name: "; cin >> Username; cout << endl;
    cout << " Welcome, " << Username << endl;
    cout << "   Guess this number!\n";
    int Ale = 0;
    while(Ale == 0){
        int const ContraC = 134, ContraC2 = 143, ContraC3 = 341, ContraC4 = 314, ContraC5 = 431, ContraC6 = 413;
        int Contra;

        cout << endl;
        cout << "  It is an integer, the three digits add up to 8, and the product of their digits is 12"; cout << endl;
        cout << "  >>>>>>> Type the password and press Enter:"; cin >> Contra;

        if(Contra == ContraC, ContraC2, ContraC3, ContraC4, ContraC5, ContraC6){
            cout << "   Good job! Now press any key to exit the game";
            Ale = 2;
            cout << endl;
            cout << endl;
        } else {
            cout << "   Try again!";
            Ale = 0;
            cout << endl;
        }
    }
    system("pause > nul");

I saw that the error is here if(Contra == ContraC, ContraC2, ContraC3, ContraC4, ContraC5, ContraC6){ because if I change it to this:我看到错误在这里if(Contra == ContraC, ContraC2, ContraC3, ContraC4, ContraC5, ContraC6){因为如果我将其更改为:

if(Contra == ContraC){

it only works with the first variable.它仅适用于第一个变量。

I was wondering how I can use more than one variable in this if.我想知道如何在这个 if 中使用多个变量。 Thank you, ralph.谢谢你,拉尔夫。

you can use switch like this to not repeat Contra (instead of || )你可以使用这样的switch来不重复Contra (而不是||

switch(Contra) {
  case ContraC:
  case ContraC2:
  case ContraC3:
  case ContraC4:
  case ContraC5:
  case ContraC6:
     cout << " Good job! Now press any key to exit the game";
     Ale = 2;
     cout << endl;
     cout << endl;
     break;
  default:
     cout << "   Try again!";
     Ale = 0;
     cout << endl;
}

for more details:更多细节:

see the docs here: https://en.cppreference.com/w/cpp/language/switch请参阅此处的文档: https://en.cppreference.com/w/cpp/language/switch

You likely want to use the ||您可能想使用|| operator.操作员。

if (Contra == ContraC || 
    Contra = ContraC2 ... ) {
  // ...
}
else {
  cout << "   Try again!";
  Ale = 0;
  cout << endl;
}

Given that you're operating on ints, you can also use a switch statement and take advantage of case fall-through.鉴于您正在对整数进行操作,您还可以使用 switch 语句并利用 case fall-through。

switch (Contra) {
  case ContraC:
  case ContraC2:
  // ...
  break;

  default:
  cout << "   Try again!";
  Ale = 0;
  cout << endl;
}

What are you trying to do?你想做什么?

If what you want is "if Contra is equal to ContraC, or Contra is equal to ContraC2, or Contra is equal to ContraC3, etc.", then you should write exactly that.如果你想要的是“如果Contra 等于ContraC,或者Contra 等于ContraC2,或者Contra 等于ContraC3,等等”,那么你应该准确地写出来。

if(Contra == ContraC || 
   Contra == ContraC2 || 
   Contra == ContraC3 || 
   Contra == ContraC4 || 
   Contra == ContraC5 || 
   Contra == ContraC6) 
{

The reason why what you did isn't a syntax error is that the comma operator exists.您所做的不是语法错误的原因是逗号运算符存在。 It doesn't look like that is what you want though.看起来这不是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM