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 循环到开始_C_Loops_If Statement_Do While - Fatal编程技术网

C 循环到开始

C 循环到开始,c,loops,if-statement,do-while,C,Loops,If Statement,Do While,嗨,我还在学C,下面是我的想法。我的问题是,如果用户输入的变量“number”小于或等于0,它就不会从头开始。有什么建议吗?还介意我问一下,在使用函数时如何循环到开头 void main() { int x = 0, y = 0, m, n, number, life = 4; char choice; int a[R][C] = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} }; do { prin

嗨,我还在学C,下面是我的想法。我的问题是,如果用户输入的变量“number”小于或等于0,它就不会从头开始。有什么建议吗?还介意我问一下,在使用函数时如何循环到开头

void main() {

    int x = 0, y = 0, m, n, number, life = 4;
    char choice;
    int a[R][C] = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };

    do {
        printf("\t\t\t      Welcome to my game");
        Sleep(2000);
        printf("\n\n   This game is all about guessing where your number is located in the matrix");
        Sleep(2000);
        printf("\n\n\t\t\t\tOnly 1 player\n");
        Sleep(2000);
        printf("\n\n   You are betting your own very life! \t\t\tYou have 5 tries");
        Sleep(1000);
        printf("\n\nEnter your number: ");
        scanf("%d", &number);
        system("cls");
        if (number <= 0) {
            system("cls");
            printf("Only positive numbers allowed");
            printf("\nRetry? Y/N: ");
            fflush(stdin);
            scanf("%c", &choice);
            fflush(stdin);
            if (choice == 'n' || choice == 'N') {
                system("cls");
                printf("Thanks for playing");
                break;
            }
            system("cls");
            break;
        }
        display(a);
        printf("\n\nInputted number is %d", number);
        printf("\n\nYour number is now being placed at a random location");
        printf("\n\nGuess where your number is located (row) (column)");
        printf("\n\nNote: Enter only numbers 1-4 or else you'll be wrong");
        printf("\n\nEnter coordinates: ");
        scanf("%d%d", &m, &n);
        printf("\n");
        placingguess(a, number, m, n, life);
        break;
    } while (choice != 'n' || choice != 'N');

    getch();
}
void main(){
int x=0,y=0,m,n,数,寿命=4;
字符选择;
int a[R][C]={0,0,0,0},{0,0,0},{0,0,0,0},{0,0,0};
做{
printf(“\t\t\t欢迎来到我的游戏”);
睡眠(2000年);
printf(“\n\n这个游戏是关于猜测你的数字在矩阵中的位置”);
睡眠(2000年);
printf(“\n\n\t\t\t\t\tOnly 1个播放器\n”);
睡眠(2000年);
printf(“\n\n您在赌自己的命!\t\t\t您有5次尝试”);
睡眠(1000);
printf(“\n\n输入您的号码:”);
scanf(“%d”和编号);
系统(“cls”);

如果(数字使用
继续
而不是
中断

    }
    system("cls"); 
    continue; 
}
display(a);

使用
继续
而不是
中断

    }
    system("cls"); 
    continue; 
}
display(a);
你用过

break; 
  • 中断
    -退出当前的
    \
    执行
    -
  • 继续
    -下一次迭代
第一个
中断
;很好,因为用户输入
N
重试, 第二个需要是
继续

您已经使用过的

break; 
  • 中断
    -退出当前的
    \
    执行
    -
  • 继续
    -下一次迭代
第一个
中断
;很好,因为用户输入
N
重试,
第二个需要是
continue

从技术上讲,您在代码中有未定义的行为,执行
fflush(stdin)
不是一个定义的操作。一些标准库允许它作为扩展,但如果你想编写可移植代码,就不应该养成这样的习惯。你真的觉得让程序休眠7000秒有意义吗?进一步说,
stdin
上的
fflush
会导致未定义的行为。什么是
>cls
?至于您的问题,循环中的那些
break
语句是什么?您知道循环中的功能吗?@user3078414
cls
是要清除的DOS命令screen@user3078414:
Sleep()
最有可能的是Win32 API的Sleep函数,它需要毫秒(注意大写的
s
).
cls
是DOS/Windows命令解释器清除控制台的命令。从技术上讲,代码中有未定义的行为,执行
fflush(stdin)
不是一个定义的操作。一些标准库允许它作为扩展,但如果你想编写可移植代码,就不应该养成这样的习惯。你真的觉得让程序休眠7000秒有意义吗?进一步说,
stdin
上的
fflush
会导致未定义的行为。什么是
>cls
?至于您的问题,循环中的那些
break
语句是什么?您知道循环中的功能吗?@user3078414
cls
是要清除的DOS命令screen@user3078414:
Sleep()
最有可能的是Win32 API的Sleep函数,它需要毫秒(注意大写的
s
).
cls
是DOS/Windows命令解释器清除控制台的命令。我以前不知道有像“继续”这样的命令。现在它工作了!谢谢。我以前不知道有像“继续”这样的命令。现在它工作了!谢谢。谢谢!现在它工作了。直到现在我才知道有像“继续”这样的命令。谢谢!现在它工作了。直到现在才知道有“继续”命令。