Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 如果用户按除1、2或3以外的任何键,如何返回开始?_C_Loops_User Input - Fatal编程技术网

C 如果用户按除1、2或3以外的任何键,如何返回开始?

C 如果用户按除1、2或3以外的任何键,如何返回开始?,c,loops,user-input,C,Loops,User Input,在下面的代码中,用户将输入1、2或3,然后由getch获取,然后程序将进入切换…但我有一个问题 在应用任何开关后,如果用户按下任何键,我想让程序回到开始 c语言对我来说仍然是新的,所以如果你能用 简单方法 #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <math.h> #include <conio.h> int main(void) {

在下面的代码中,用户将输入1、2或3,然后由getch获取,然后程序将进入切换…但我有一个问题 在应用任何开关后,如果用户按下任何键,我想让程序回到开始

c语言对我来说仍然是新的,所以如果你能用 简单方法

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>

int main(void) {
    char L;

    while (1) {
        L = getch();

        switch (L) {
        case '1':
            system("cls"); 
            printf("111111111111111");
            break;
        case '2':
            system("cls");
            printf("222222222222222");
            break;
        case '3':
            system("cls");
            printf("33333333");
            break;
        default:
            sleep(0);
        }
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
内部主(空){
查尔L;
而(1){
L=getch();
开关(L){
案例“1”:
系统(“cls”);
printf(“111111111”);
打破
案例“2”:
系统(“cls”);
printf(“22222222”);
打破
案例“3”:
系统(“cls”);
printf(“33333333”);
打破
违约:
睡眠(0);
}
}
返回0;
}

您可以使用
while
循环来包装开关盒。例如:

int done = 0;
while(!done) {
    L = getch();
    switch (L) {
    case '1' :
        system("cls"); 
        printf("111111111111111");
        done = 1;
        break;
    case '2' :
        system("cls");
        printf("222222222222222");
        done = 1;
        break;
    case '3' :
        system("cls");
        printf("33333333");
        done = 1;
        break;
    default :
        sleep(0);
    }
}

使用
do
-
while
循环并将条件设置为
while(L!=“1”和&L!=“2”和&L!=“3”)

上面的代码将首先提示输入数据,然后对其进行评估,并继续这些操作,直到用户输入
'1'
'2'
'3'


如下面的注释所述,使用
睡眠(0)
默认值
情况没有任何用处,但可以改进,例如:

do {
    L = getch();
    switch (L) {
    case '1' :
        system("cls"); 
        printf("111111111111111");
        break;
    case '2' :
        system("cls");
        printf("222222222222222");
        break;
    case '3' :
        system("cls");
        printf("33333333");
        break;
    default :
        L = 0;
    }
} while(!L);
在本例中,您不必仔细检查允许的值(假设您不希望在
L==0
时使用这种情况)


因为你问题的标题有点误导,也许我不明白你想要达到什么。如果您只想暂停返回循环的开始,可以在
开关
块之后添加额外的
getch()

while (1) {
    L = getch();

    switch (L) {
    case '1':
        system("cls"); 
        printf("111111111111111");
        break;
    case '2':
        system("cls");
        printf("222222222222222");
        break;
    case '3':
        system("cls");
        printf("33333333");
        break;
    }

    printf("Press Any Key to Continue...");
    getch();
}

如果这仍然不能回答您的问题,我建议您改进。

缩进…………请编辑问题并缩进代码。请编辑问题并缩进代码。现在,很难理解你想要什么!我很好奇投票失败的原因。睡眠(0)
的原因是什么?(BTW我赞成)。“MichaelWalz只是抄袭了这个问题,没有考虑。”MichaelWalz睡了0秒,没用。可能它使用sleep(0)只是为了在default case上有一个语句。@marteny如果您不想在
default
case中做任何事情,那么就不要将它插入到
switch
语句中。一个
开关
没有
默认值
大小写是可以的。为什么要下一票?在任何大小写被应用后,如果用户按下任何键,程序将返回到开始…@martenmarteny当用户按下除1、2或3以外的任何键时,程序返回到开始时,你给你的问题起了标题。但看起来你想要更简单和不同的东西。如果您想在按键后返回,只需在
while
循环的末尾添加一个额外的
getch()
while (1) {
    L = getch();

    switch (L) {
    case '1':
        system("cls"); 
        printf("111111111111111");
        break;
    case '2':
        system("cls");
        printf("222222222222222");
        break;
    case '3':
        system("cls");
        printf("33333333");
        break;
    }

    printf("Press Any Key to Continue...");
    getch();
}