C程序: if statement within switch statement

标签 c

我正在尝试创建一个可以在“模式”之间切换的程序。例如,这里是一小段代码:

int main()
{
int mode,input;
mode = 1;
   for(;;)
   {
   scanf("%d", &input);
      switch(input)
      case 1: 
         if(mode = 1)
         {
         //statements go here;
         mode = 2;
         }
         else
         {
         //statements go here;
         mode = 1;
         }
      break;
   }
}

所以我想做的是让程序通过1按钮的输入在模式1和模式2之间切换。但是,每次按数字1键时,它只会打印模式1的语句,而不会切换到模式2,如果我第二次按数字1键,则不会打印模式2的语句。我的代码有什么根本性的错误吗?

*限制:我必须在程序中使用switch语句。

最佳答案

要测试变量的值,您需要双等于:

if(模式==1)

关于C程序: if statement within switch statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49102240/

相关文章:

c++ - C/C++ : Reading and writing time stamp data to file with multi architecture support

c - 用消息队列C发送文件内容

c - 我的代码有什么问题?多维数组? C编程

c - 哈希表打印有点乱

c - 使用 C 中的函数从文件读取结构体数组

c - 如何交换两个字符串的第一个字符?两个字符串存储在指针数组中

C定义函数?

c - 链表给出相同的结果 C

c - 为什么 open() 在 linux 1.0 中没有返回 'fd'?

c - 关于用 C 中的用户输入填充数组的问题