WHCSRL 技术网

C语言练习2---猜数字小游戏

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. void Print();
  5. int guess();
  6. void End(int count);
  7. enum chonice
  8. {
  9. EXIT,
  10. PLAY,
  11. AGAIN
  12. };
  13. int main()
  14. {
  15. srand((unsigned)time(NULL));
  16. int input = 0;
  17. int count=0;
  18. Print();
  19. do
  20. {
  21. scanf("%%%%d", &input);
  22. switch (input)
  23. {
  24. case EXIT:
  25. printf("退出游戏成功!!! ");
  26. break;
  27. case PLAY:
  28. case AGAIN:
  29. printf("请输入您所猜的数字! ");
  30. count=guess();
  31. End(count);
  32. break;
  33. default:
  34. printf("输入有误,请输入菜单栏选项 ");
  35. break;
  36. }
  37. } while (input!=0);
  38. system("pause");
  39. return 0;
  40. }
  41. void Print()
  42. {
  43. printf("*******祝您游戏愉快******** ");
  44. printf("*************************** ");
  45. printf("******1.开始游戏*********** ");
  46. printf("*************************** ");
  47. printf("******0.退出游戏*********** ");
  48. printf("*************************** ");
  49. printf("******2.再玩一次*********** ");
  50. printf("*************************** ");
  51. }
  52. int guess()
  53. {
  54. int a = 0;
  55. int count = 0;
  56. int b = rand() %%%% 100 + 1;
  57. while (scanf("%%%%d", &a), b != a)
  58. {
  59. if (a <= (a + b) / 2)
  60. {
  61. printf("猜小了!");
  62. printf("再猜猜? ");
  63. count++;
  64. }
  65. else
  66. {
  67. printf("猜大了!");
  68. printf("再猜猜? ");
  69. count++;
  70. }
  71. }
  72. return count+1;
  73. }
  74. void End(int count)
  75. {
  76. printf("恭喜您猜对了,总共猜了%%%%d次 ", count);
  77. printf("是否再玩一次? ");
  78. printf("*************************** ");
  79. printf("******0.退出游戏*********** ");
  80. printf("*************************** ");
  81. printf("******2.再玩一次*********** ");
  82. printf("*************************** ");
  83. }

推荐阅读