循环开始

时间:2016-07-16 12:47:33

标签: 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 {
        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();
}

2 个答案:

答案 0 :(得分:3)

使用continue代替break

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

答案 1 :(得分:2)

您已使用

break; 
  • break - 退出当前while \ for \ do - while ...
  • continue - 下一次迭代。

第一个break;很好,因为用户输入N进行重试, 第二个需要continue