如何在案例2发生之前在案例2中进行切换 [英] How do I make a switch in C where case 2 cant happen before case 1 madem

查看:107
本文介绍了如何在案例2发生之前在案例2中进行切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说:

我想在案件1发生之前使案例2无法发生。

Lets say I have:
And I want to make case 2 unable to happen before case 1 has happend.

int opt = -1;
while(opt != -1){
printf("1. for stuff. 2. for other stuff);
scanf("%d", &opt);
switch(opt){
case 1:
Randomize(vek);
break;
case 2:
sort(vek);
break;
defualt:
printf("Dont exist");
}
}





我的尝试:





What I have tried:

<pre lang="C">
typedef bool;
#define true 1
#define false 1
int opt = -1;
bool test = false;
while(opt != -1){
printf("1. for stuff. 2. for other stuff);
scanf("%d", &opt);
switch(opt){
case 1:
Randomize(vek);
test=true;
break;
if(test = true){
case 2:
sort(vek);
break;
}
defualt:
printf("Dont exist");
}
}

推荐答案

试试这个:

Try this:
while(opt != -1)
   {
   printf("1. for stuff. 2. for other stuff);
   scanf("%d", &opt);
   switch(opt)
      {
      case 1:
         Randomize(vek);
         test=true;
         break;
      case 2:
         if(test == true)
            {
            sort(vek);
            }
         break;
      default:
         printf("Dont exist");
      }
   }


如果 c>你不能插入 code>改变开关的结构

开关可以只处理最基本的条件:a变量是否与值匹配。



您需要使用 if elseif else 结构。

变量 test 要记住选项1已完成是要走的路。



这里是语言作者的C和C ++参考书的链接。注意,C是C ++的祖先,所以知道C对C ++总是有用。

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2。 pdf [ ^ ]

http://www.ime.usp。 br / ~pf / Kernighan-Ritchie / C-Programming-Ebook.pdf [ ^ ]



C ++编程语言 [ ^ ]
You can't insert a if to alter the structure of a switch.
The switch can handle only the most basic condition: a variable match a value or not.

You need to use a if elseif else structure.
The variable test to remember that option 1 is done is the way to go.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]


这篇关于如何在案例2发生之前在案例2中进行切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆