#include#include #include void print_menu() { puts("**********************"); puts("**** 1. Start ********"); puts("**** 0. Exit ********"); puts("**********************"); } int main() { srand((unsigned)time(NULL)); // 僅一次! int choice = 1; puts("Welcome to Guess Number Game!"); while (choice) { print_menu(); printf("Choice: "); if (scanf("%d", &choice) != 1) { while (getchar() != '\n'); puts("Invalid input!"); continue; } switch (choice) { case 1: { int target = rand() % 101; int guess; while (1) { printf("Guess (0-100): "); if (scanf("%d", &guess) != 1) { while (getchar() != '\n'); puts("Numbers only!"); continue; } if (guess > target) puts("Too large!"); else if (guess < target) puts("Too small!"); else { puts("Correct!"); break; } } break; } case 0: puts("Bye!"); break; default: puts("Invalid option!"); } } return 0; }
猜字遊戲是 C 語言入門綜合練習:標準庫、隨機數、輸入驗證、選單迴圈、函式分離。掌握本範例結構,即具備開發文字介面互動程式的核心能力。