C++ 使用或切换语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4704986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch statement using or
提问by Saladin Akara
I'm creating a console app and using a switch
statement to create a simple menu system. User input is in the form of a single character that displays on-screen as a capital letter. However, I do want the program to accept both lower- and upper-case characters.
我正在创建一个控制台应用程序并使用一个switch
语句来创建一个简单的菜单系统。用户输入采用单个字符的形式,在屏幕上显示为大写字母。但是,我确实希望程序接受小写和大写字符。
I understand that switch
statements are used to compare against constants, but is it possible to do something like the following?
我知道switch
语句用于与常量进行比较,但是否可以执行以下操作?
switch(menuChoice) {
case ('q' || 'Q'):
//Some code
break;
case ('s' || 'S'):
//More code
break;
default:
break;
}
If this isn't possible, is there a workaround? I really don't want to repeat code.
如果这是不可能的,是否有解决方法?我真的不想重复代码。
回答by Chris Hasiński
This way:
这边走:
switch(menuChoice) {
case 'q':
case 'Q':
//Some code
break;
case 's':
case 'S':
//More code
break;
default:
}
More on that topic: http://en.wikipedia.org/wiki/Switch_statement#C.2C_C.2B.2B.2C_Java.2C_PHP.2C_ActionScript.2C_JavaScript
有关该主题的更多信息:http: //en.wikipedia.org/wiki/Switch_statement#C.2C_C.2B.2B.2C_Java.2C_PHP.2C_ActionScript.2C_JavaScript
回答by John Parker
The generally accepted syntax for this is:
普遍接受的语法是:
switch(menuChoice) {
case 'q':
case 'Q':
//Some code
break;
case 's':
case 'S':
//More code
break;
default:
break;
}
i.e.: Due the lack of a break
, program execution cascades into the next block. This is often referred to as "fall through".
即:由于缺少 a break
,程序执行级联到下一个块。这通常被称为“跌倒”。
That said, you could of course simply normalise the case of the 'menuChoice' variable in this instance via toupper/tolower.
也就是说,您当然可以简单地通过 toupper/tolower 将本例中“menuChoice”变量的大小写标准化。
回答by BlackBear
Just use tolower()
, here's my man:
只需使用tolower()
,这是我的男人:
SYNOPSIS
#include ctype.hint toupper(int c); int tolower(int c);
DESCRIPTIONtoupper() converts the letter c to upper case, if possible.
tolower() converts the letter c to lower case, if possible. If c is not an unsigned char value, or EOF, the behavior of these functions is undefined.
RETURN VALUEThe value returned is that of the converted letter, or c if the conversion was not possible.
概要
#include ctype.hint toupper(int c); int tolower(int c);
说明在toupper()转换字母c为大写,如果可能的话。
tolower() converts the letter c to lower case, if possible. If c is not an unsigned char value, or EOF, the behavior of these functions is undefined.
返回值 返回值是转换后的字母的值,如果无法转换,则返回 c。
So in your example you can switch()
with:
因此,在您的示例中,您可以switch()
使用:
switch(tolower(menuChoice)) {
case('q'):
// ...
break;
case('s'):
// ...
break;
}
Of course you can use both toupper()
and tolower()
, with capital and non-capital letters.
当然,您可以同时使用toupper()
和tolower()
,以及大写和非大写字母。
回答by There is nothing we can do
You could (and for reasons of redability, should) before entering switch statement use tolower fnc on your var.
在输入 switch 语句之前,您可以(并且出于可红性的原因,应该)在您的 var 上使用 tolower fnc。
回答by Bojan Komazec
'q' || 'Q'
results in booltype result (true) which is promoted to integral type used in switch condition (char) - giving the value 1. If compiler allowed same value (1) to be used in multiple labels, during execution of switchstatement menuChoice
would be compared to value of 1 in each case. If menuChoice
had value 1 then code under the first case label would have been executed.
'q' || 'Q'
导致bool类型结果 ( true) 被提升为用于 switch 条件 ( char) 的整数类型- 给出值 1。如果编译器允许在多个标签中使用相同的值 (1),则在switch语句的执行期间menuChoice
将进行比较在每种情况下都为 1。如果menuChoice
值为 1,则将执行第一个 case 标签下的代码。
Therefore suggested answers here use character constant (which is of type char) as integral value in each case label.
因此,此处建议的答案使用字符常量(其类型为char)作为每种情况标签中的整数值。
回答by Edward Strange
switch (toupper(choice))
{
case 'Q':...
}
...or tolower.
...或更低。
回答by eyong kevin
if you do
如果你这样做
case('s' || 'S'):
// some code
default:
// some code
both s
and S
will be ignored and the default code will run whenever you input these characters. So you could decide to use
双方s
并S
会被忽略,默认的代码将运行,只要你输入这些字符。所以你可以决定使用
case 's':
case 'S':
// some code
or
或者
switch(toupper(choice){
case 'S':
// some code.
toupper
will need you to include ctype.h
.
toupper
将需要您包括ctype.h
.