Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 此循环在满足条件时不会结束_C_While Loop - Fatal编程技术网

C 此循环在满足条件时不会结束

C 此循环在满足条件时不会结束,c,while-loop,C,While Loop,我试图编写一个程序来获取一个字符,然后检查并打印它是大写还是小写。然后我希望它继续循环,直到用户输入一个“0”,它应该会产生一条消息。不起作用的是底部的while条件,而该条件似乎从未得到满足 #include <stdio.h> #include <stdlib.h> int main() { int ch,nope; // Int ch and nope variable used to terminate program do /

我试图编写一个程序来获取一个字符,然后检查并打印它是大写还是小写。然后我希望它继续循环,直到用户输入一个“0”,它应该会产生一条消息。不起作用的是底部的while条件,而该条件似乎从未得到满足

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int ch,nope;    // Int ch and nope variable used to terminate program
    do       // start 'do' loop to keep looping until terminate number is entered
    {
        printf("Enter a character : ");             // asks for user input as a character
        scanf("%c",&ch);                            // stores character entered in variable 'ch'
        if(ch>=97&&ch<=122) {                       // starts if statement using isupper (in built function to check case)
            printf("is lower case\n");
        } else {
            printf("is upper case\n");
        }
    }
    while(ch!=0);                                     // sets condition to break loop ... or in this case print message
    printf("im ending now \n\n\n\n\n",nope);     // shows that you get the terminate line

}
#包括
#包括
int main()
{
int-ch,nope;//用于终止程序的int-ch和nope变量
do//启动“do”循环以保持循环,直到输入终止编号
{
printf(“输入字符:”;//要求用户以字符形式输入
scanf(“%c”,&ch);//存储变量“ch”中输入的字符
如果(ch>=97&&ch尝试
而(ch!=48);
因为48是字符“0”的十进制数。 正如Maroun Maroun提到的,while(ch!=“0”)更容易理解

如果不希望在用户输入“0”时显示大写消息,可以执行以下操作:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned char ch,nope;    // Int ch and nope variable used to terminate program
    while (1)
    {
        printf("Enter a character : ");             // asks for user input as a character
        scanf("%c",&ch);                            // stores character entered in variable 'ch'
        if(ch==48) {
            break;
        }
        if(ch>=97&&ch<=122) {                      // starts if statement using isupper (in built function to check case)
            printf("is lower case\n");
        } else {
            printf("is upper case\n");
        }

    }
    printf("im ending now \n\n\n\n\n",nope);     // shows that you get the terminate line

}
#包括
#包括
int main()
{
unsigned char ch,nope;//用于终止程序的Int ch和nope变量
而(1)
{
printf(“输入字符:”;//要求用户以字符形式输入
scanf(“%c”,&ch);//存储变量“ch”中输入的字符
如果(ch==48){
打破
}

如果(ch>=97&&ch
ch!=0
是错误的,那么应该是
ch!='0'
魔法数字97和122是个坏主意。使用
'a'
'z'
,或者调用
中声明的
islower()
。此外,代码将报告“是大写的”对于数字和标点符号。这很有效!太棒了。但出于某种原因,它会打印大写语句以及终止语句。这是因为您要检查的条件发生在while循环的末尾。首先执行代码,然后检查条件。我添加了完整的代码,因此它将退出im当用户输入零并且不检查外壳时,立即进行。