CEZJ_ 2022-11-02 17:43 采纳率: 71.4%
浏览 30
已结题

一个简单的随机数游戏

问题遇到的现象和发生背景 游戏目的是:电脑随机生成一个数,然后玩家输入一个数,看几次可以相同。我想要做到一个人重复四次游戏,且一次游戏里一直猜测直到相同。
用代码块功能插入代码,请勿粘贴截图
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    srand((unsigned int)time(NULL)*10);
    int num1 = rand() %32 + 1;
    int num2;
    int cs = 0;
    int game = 0;
    int best;
    int last;
    if (game < 4)
    {printf("Please enter a number in 1-32, which you think is the same with computer:\n");
     scanf("%d",&num2);
     if (num1 == num2)
     {
        cs++;
        printf("You are correct!\n");
     }
     else
     {
        cs++;
        printf("You are wrong!Try again.\n");
        do{
             printf("Please enter a number, which you think is the same with computer:\n");
             scanf("%d",&num2);
             printf("You are wrong again!");
             cs++;
        } while(num1 == num2);
       
      } 
         printf("Your grades is %d.\n",cs);
        printf("Write down your last time best grade :");
        scanf("%d",&last);
        if (cs < last)
        {
            best = cs;
            printf("Your best score now is %d.",best);
        }
        else
        {
            best = last;
            printf("Your best score now is %d.",best);
        }
     }
     else
     {
        printf("You have finished this game now!\n Here is your best grade:%d.\n",best);
     }
     

    
    return 0;

}

运行结果及报错内容 玩家输入两次后就结束了,没有一直在循环里,不知道为什么
我的解答思路和尝试过的方法
我想要达到的结果 玩家一直循环,直到猜中当局随机数
  • 写回答

3条回答 默认 最新

  • 叶落花枯 2022-11-02 17:50
    关注

    31行,num1 != num2
    不等于就继续循环,等于就退出

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月10日
  • 已采纳回答 11月2日
  • 创建了问题 11月2日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题