C语言 使用带有数字大小写和字符大小写的开关
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40678930/
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
Use switch with number case and char case
提问by Someonewhohaveacat
I want to make a switchloop.
我想做一个switch循环。
If input is from 1to 5. It will print a number. Else it will print "this is not a number". And if the input is 'e'. The switchloop should be ended.
如果输入是 from1到5. 它会打印一个数字。否则它会打印"this is not a number"。如果输入是'e'. 该switch循环应该结束。
I can make the number part, but I don't know how can I to make input with 'e'. It just won't read. Here is my code:
我可以制作数字部分,但我不知道如何使用'e'. 就是不会读。这是我的代码:
int main() {
int i,a = 0;
printf("Write something:");
scanf("%d", &i);
do{
switch (i)
{
case 1:
printf("1");
break;
case 2:
printf("2");
break;
case 3:
printf("3");
break;
case 4:
printf("4");
break;
case 5:
printf("5");
break;
case 'e':
a=1;
break;
default:
printf("This is not a number”);
break;
}
}while (a==0);}
My problem is I can't have input as 'e'or any char. Because if I do that I will create a paradox loop or just don't work at all. Where am I wrong?
我的问题是我不能输入 as'e'或任何字符。因为如果我这样做,我会创建一个悖论循环或者根本不起作用。我哪里错了?
回答by Christopher Schneider
When you say 'e', you are actually saying "The value of 'e'". This is the ASCII value. The decimal value of 'e' is actually 101.
当你说 'e' 时,你实际上是在说“'e' 的值”。这是 ASCII 值。'e' 的十进制值实际上是 101。
To make this work, you could use the chardatatype. Read in the data as a char, and in your switch statement, instead of a digit, use a char instead. e.g.
要完成这项工作,您可以使用char数据类型。将数据作为字符读入,并在您的 switch 语句中使用字符而不是数字。例如
switch (i)
{
case '1':
//do something
break;
回答by cleblanc
Just treat the integers as characters like this;
只需将整数视为这样的字符即可;
By the way, you probably want to read a new character each time through the while loop, otherwise you'll get stuck printing forever.
顺便说一句,您可能希望每次通过 while 循环读取一个新字符,否则您将永远无法打印。
int main() {
char i,a = 0;
printf("Write something:");
scanf("%c", &i);
do{
switch (i)
{
case '1':
case '2':
case '3':
case '4':
case '5':
printf("%c",i);
break;
case 'e':
a=1;
break;
default:
printf("This is not a number”);
break;
}
}while (a==0);
}
回答by cokceken
You can make your switch as switch(char). Convert your input to char(since it is 1 to 5 it can be a char). Then check for case '1':case '2'etc.
您可以将开关设为switch(char). 将您的输入转换为char(因为它是 1 到 5,所以它可以是一个字符)。然后检查case '1':case '2'等。
Edit: you need to take your input as a char
编辑:您需要将输入作为字符
char i;
int a = 0
printf("Write something:");
scanf("%c", &i);
then
然后
switch(i){
回答by F.Huet
You can't do this because in your scanf you specify that the input will be a decimal (%d). If you want to check if it's a number or not, you have to deal with chars. for example :
您不能这样做,因为在您的 scanf 中您指定输入将是小数 (%d)。如果你想检查它是否是一个数字,你必须处理字符。例如 :
char c;
while (42)
{
scanf("%c", &c);
if (getnbr(c) < 1 && getnbr(c) > 5)
printf("%c", c);
else
printf("This is not a number”);
}
Something like that ... Hope i helped.
类似的东西......希望我有所帮助。
回答by cdcdcd
Others have suggested using %c so that your not mixing characters with integers but you need to be careful with the rest of the code. You may need a cast for the switch statement.
其他人建议使用 %c 以便您不要将字符与整数混合,但您需要小心其余代码。您可能需要对 switch 语句进行强制转换。
int i;
printf("Input:\n");
scanf(" %c", &i);
switch ((char)i)
{
case '1':
printf("Got number 1\n");
break;
case '2':
printf("Got number 2\n");
break;
case 'e':
printf("GREAT\n");
break;
}
EDIT: Karston Koop asked a good question. My reasoning for the cast is simply that the integer i is not initialised. The formatted write from the scanf will only initialise the lowest/highest (depending on the hardware) byte of the entire 4bytes of the integer - the rest could be filled with garbage depending on what is on the 4bytes of stack reserved for the integer variable i. If it were a char all the bits would be initialised by scanf. As a result we only want the lower/upper byte for the switch statement as is and hence the cast. To get around this you could just initilaise i = 0. Then the cast becomes superfluous. I'll leave this in as it might be useful to someone in the future. Thanks to Karston for quizzing me on this.
编辑:Karston Koop 问了个好问题。我对转换的推理只是整数 i 没有初始化。来自 scanf 的格式化写入只会初始化整数的整个 4 字节的最低/最高(取决于硬件)字节 - 其余的可能会被垃圾填充,具体取决于为整数变量 i 保留的 4 字节堆栈上的内容. 如果它是一个字符,所有的位都将由 scanf 初始化。因此,我们只需要 switch 语句的低/高字节,因此需要转换。为了解决这个问题,你可以初始化 i = 0。然后演员表就变得多余了。我将保留它,因为它可能对将来的某人有用。感谢 Karston 在这方面对我进行了测验。

