为什么我的 if/else 语句没有产生预期的结果?

Why isn't my if/else statement producing the desired results?

本文关键字:结果 if 我的 else 语句 为什么      更新时间:2023-10-16

我的程序有问题。我实际上已经完成了它,但最后并没有正常工作。我用两种方式编写了它,一种是使用 switch 语句,另一种是使用 if else 语句,但两者都无法正常工作。它们都为我编译并具有干净的构建,但不会按预期运行。

#include <iostream> 
#include <cstdio>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
int main()
{
    int i = 0;
    const int size = 27;
    char newline = 'n';
    char *pnumber = 0;
    int selection = 0;
    cout << "PRINGING CONTENTS OF ARRAY" << endl;
    cout << "====================================================" << endl;
    char alphabet[size] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'
    , 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'
    , 'Z' , 0 };
     for (i = 0; i < size; i++)
     {
         cout << alphabet[i] << " ";
     }
     cout << newline;
     cout << newline;
     cout << "This is the title to your Program related to the alphabet."
     << endl;
     cout << endl;
     cout << "Select the index that coincides with the alphabet." << endl;
     cout << "For example, the number 7 should display the letter G" << endl;
     cout << endl;
     cout << "Enter an index between 1 and 26: ";
     cin >> i;
     cout << "The number you selected: " << i;
     cout << newline;
     cout << "The letter at this index: " << alphabet[i - 1];
     cout << endl;
     cout << newline;
     cout << newline;
     cout << "PRINTING CONTENTS OF ARRAY and adding x to every other element" << endl;
     pnumber = &alphabet[1];
     for (i = 0; i < size; i += 1)
     {
         if (i % 2 == 0)
         {
             cout << alphabet[i] << " ";
         }
    else
         {
             cout << "x ";
         }
     }
     cout << newline;
     cout << newline;
     cout << "PRINTING CONTENTS OF ARRAY USING THE MOD Option" << endl;
     cout << "=========================================================" << endl;
     cout << "Do you want the letters of the alphabet starting from index 0, A, or index 1, B: ";
     cin >> selection;
      switch (selection)
     {
         case 0:
         {
             for (i = 0; i < size; i += 2)
             {
                 cout << "Even Numbered Elements=" << i << " ";
                 cout << "Contents of Element within Array is=" << alphabet[i];
                 break;
             }
         }
         case 1:
         {
             for (i = 1; i < size; i += 2)
             {
                 cout << "Odd Numbered Elements=" << i << " ";
                 cout << "Contents of Element within Array is=" << alphabet[i] << endl;
                 break;
         }
     }
     default: cout << "You entered an invalid esponse.";
     }
     cout << endl;
     return 0;
 } //End of Int Main

如果其他的话,最后一部分是这样的:

cout << newline;
cout << newline;
cout << "PRINTING CONTENTS OF ARRAY USING THE MOD Option" << endl;
cout << "=========================================================" << endl;
cout << "Do you want the letters of the alphabet starting from index 0, A, or index 1, B: ";
cin >> selection;
if (selection = 0){
    for (i = 0; i < size; i += 2)
    {
        cout << "Even Numbered Elements=" << i << " ";
        cout << "Contents of Element within Array is=" << alphabet[i] << endl;
    }
}
else{
    for (i = 1; i < size; i += 2)
    {
        cout << "Odd Numbered Elements=" << i << " ";
        cout << "Contents of Element within Array is=" << alphabet[i] << endl;
    }
}
return 0;
} //End of Int Main

1(第一部分输出字母表,2(接下来它提示输入一个数字并给出相关的字母,3(接下来程序应该输出,其他每个字母都是x,例如A x C x E x.......。4(最后,它应该提示用户选择O以从A或1开始字母表并启动B,然后它将每隔一个字母输出一次。最后一部分不起作用。它不提供选项,而是每次都做同样的事情。我已经以其他方式对其进行了调整,它两次都会做一个或另一个,而不是一个或另一个。我做错了什么?

对于第二部分更改

if (selection = 0){

if (selection == 0){

表达式selection = 0是一个赋值,但您需要比较。

此外,为了检查均匀度,您可以使用%运算符。 例如:

      if (selection % 2 == 0)
      {
           // if even
      }
      else
      {
           // if odd
      }

编辑:

为了避免将来出现此类错误类型,请训练自己在比较运算符的左侧写入常量值,例如

 if (0 == selection)

如果你输入错误,编译器将显示有关L值的错误,该桅杆是可变的。

编辑 2(关于开关和用于(:

在下面的代码中

switch (selection)
{
   case 0:
         {
             for (i = 0; i < size; i += 2)
             {
                 cout << i << " ";
                 break;
             }
         }
    case 1:
     ...
}

break语句指的是for,而不是switch,因此:

1( for将只执行一次;

2(虽然selection 0 case 1无论如何都会在case 0后工作。

正确的代码必须如下所示

switch (selection)
{
   case 0:
         {
             for (i = 0; i < size; i += 2)
             {
                 cout << i << " ";
             }
             break;
         }
    case 1:
     ...
}

请进行更改#

if (selection == 0)

我认为上述更改可以解决问题。在您的代码中,您不是比较而是在其中分配 0,因此永远不会希望 if 被执行并放入赋值如果不是标准。

希望这会有所帮助。