A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Q曲 中级黑马   /  2015-11-16 19:58  /  1176 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*
  2. 使用随机数产生一个1-1000的数,给用户10次竞猜的机会
  3. */
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include <time.h>

  7. void test1(){
  8. //定义两个变量
  9. int computer=-1,player=-1;

  10. //系统产生一个随机数
  11. srand(time(0));
  12. computer=rand()%3;
  13. printf("%d\n",computer);   //2

  14. //玩家输入一个数字
  15. printf("请输入:0石头  1剪刀   2布\n");
  16. scanf("%d",&player);
  17. //比较大小 玩家     系统    赢得情况
  18. //          0        1
  19. //          1        2
  20. //          2        0
  21. if((player==0 && computer==1)||
  22.     (player==1 && computer==2)||
  23.         (player==2 && computer==0)){

  24. printf("玩家赢\n");
  25. }
  26. //玩家输
  27. else if((computer==0 && player==1)||
  28.                  (computer==1 && player==2)||
  29.                  (computer==2 && player==0)){

  30. printf("玩家输了\n");
  31. }
  32. //平局
  33. else{

  34. printf("平局\n");

  35. }
  36. }
  37. int main(){

  38. //定义用户猜的数
  39. //系统随机的数
  40. //竞猜的总次数
  41. //现在已经竞猜的次数
  42. //数字的范围
  43. int userNum=0;
  44. int comNum=0;
  45. int total=10;
  46. int nowTotal=0;
  47. int m,n;

  48. //系统产生随机数
  49. srand(time(0));
  50. comNum=rand()%1000+1;
  51. printf("%d\n",comNum);   

  52. //用户输入一个数
  53. printf("请输入1-1000之间的数字:\n");
  54. scanf("%d",&userNum);
  55. //判断竞猜的次数   
  56. while(1){
  57. //可以竞猜,判断大小,
  58.         if(userNum<comNum){
  59.         printf("你输入的数字小了,请重输!\n这是第%d次输入",nowTotal);
  60.         }else if(userNum>comNum){
  61.                 printf("你输入的数字大了,请重输!\n这是第%d次输入",nowTotal);
  62.         }else(userNum=comNum){
  63.                 printf("恭喜你,猜对了\n这是第%d次输入",nowTotal);
  64.      nowTotal++;
  65.    
  66. }
  67.      
  68. //不可以竞猜,提示退出
  69. printf("你的次数已经用完,再见~!\n");

  70. return 0;
  71. }
复制代码




我是拿visualC++6.0 写的代码,可能和Xcode有些不同。

8 个回复

倒序浏览
666666666666666
回复 使用道具 举报
好牛逼 666666
回复 使用道具 举报
赞一个,程序媛同学,加油
回复 使用道具 举报
{:2_30:}{:2_30:}{:2_30:}{:2_30:}
回复 使用道具 举报
加油加油
回复 使用道具 举报
  1. /*

  2. 剪刀石头布游戏规则:项目:1⃣️剪刀 0;2⃣️石头 1;3⃣️布 2.

  3.   ① 剪刀 --> 布

  4.   ② 石头 --> 剪刀

  5.   ③ 布   --> 石头

  6.   ④ 平局

  7. 计算机computer:随机出拳 剪刀、石头、布

  8. 玩家player  :随机出拳 剪刀、石头、布

  9. 附:随机数函数

  10.   arc4random_uniform();

  11.   使用方法:

  12.   首先导入头文件:

  13.   #include <stdlib.h>

  14.   arc4random_uniform(3); //随机产生 0 1 2

  15. */

  16. #include <stdio.h>

  17. #include <stdlib.h>

  18. int main(int argc, const char * argv[]) {

  19. //    定义变量,保存计算机、用户出的随机的项目
  20.    
  21.     int computer,player;
  22.    
  23. //    计算机随机输出项目
  24.    
  25.     computer = arc4random_uniform(3);
  26.    
  27. //    printf("%d\n",computer);
  28.    
  29. //    提示用户输出项目
  30.    
  31.     printf("请出拳:0.剪刀  1.石头  2.布\n");
  32.    
  33. //    保存用户输出项目
  34.    
  35.     scanf("%d\n",&player);
  36.    
  37. //    校验用户输入是否合法
  38.    
  39.     if (player < 0 || player > 2) {
  40.         
  41.         printf("请按套路出拳!\n");
  42.         
  43.         return 0;
  44.         
  45.     }
  46.    
  47. //    根据游戏规则,判断输赢
  48.         
  49. //    输出用户输出项目的输赢结果
  50.             
  51.         if ((player == 0 && computer == 2) ||
  52.         
  53.             (player == 1 && computer == 0) ||
  54.         
  55.             (player == 2 && computer == 1)){
  56.             
  57.             printf("我靠!你赢啦!!\n");
  58.             
  59.         } else
  60.             
  61.          if ((computer == 0 && player == 2) ||
  62.                
  63.             (computer == 1 && player == 0) ||
  64.                
  65.             (computer == 2 && player == 1)){
  66.                
  67.                 printf("你输了\n");
  68.             
  69.             } else {
  70.             
  71.                 printf("平局\n");
  72.             
  73.             }
  74.    
  75.     return 0;

  76. }
复制代码


这样写,运行程序,每次都要出拳两次才有结果,什么原因?
回复 使用道具 举报
看上去很牛逼的样子
回复 使用道具 举报
hei军 中级黑马 2015-11-18 21:40:21
9#
恩恩    就是    感觉代码有点多
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马