问世间学为何物 2020-05-07 17:18 采纳率: 0%
浏览 285
已采纳

C++求助,此代码运行时会经常出现运行不下去的情况,按键盘任意键都无反应,无报错无警告,调试显示“无法查找或打开 PDB 文件”,怎么办?

#include <time.h>
#include<stdlib.h> //rand()函数所在头文件
#include<iostream>
using namespace std;
enum Gamestatus{win,lose,tie};
Gamestatus status;
////////////////定义用户出石头,剪刀,布时所对应的数字,将数字转化成对应的石头剪刀布输出           
void display(int a)
{   
switch(a)
{
case 0:
    {cout<<"石头"<<endl;}
    break;
case 1:
    {cout<<"剪刀"<<endl;}
    break;
case 2:
    {cout<<"布"<<endl;}
}
}

///////////////////////////////////////////////判断输赢
int judge(int x,int y)
{
        Gamestatus status;
    switch(x)
{
case 0:
 if(y==0){
            cout<<"平局"<<endl;

                status=tie;
        }else if(y==1){         
            cout<<"你赢一局";
            cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
                status=win;
        } else if(y==2){
            cout<<"你输一局"<<endl;
            cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
            status=lose;    
        }
        break;//一定要加break!!!!!!要检查!!!!!
    case 1:
        if(y==1){

            cout<<"平局"<<endl;
            cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
                status=tie;

        }else if(y==0){         
            cout<<"你输一局"<<endl; 
            cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
                status=lose;


        } else if(y==2){
            cout<<"你赢一局"<<endl;
            cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
            status=win;

        }
        break;
    case 2:
        if(y==2){
            cout<<"平局"<<endl;
            cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
                status=tie;

        }else if(y==1){

            cout<<"你输一局"<<endl;
            cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
                status=lose;


        }else if(y==0){
             cout<<"你赢一局"<<endl;
             cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
             status=win;


        }
}
    return status;//把status值返回给调用它的Play函数
}


int Play(){
int x;
srand((unsigned)time(NULL));//表示以当前时间对应的int值为随机序列起点,这样每次运行程序,由于起点不同得到不同的随机数
      int y = rand()%3;//产生随机数0,1,2,如果想产生的是1,2,3就加1
cout<<"请出拳:"<<endl;
cin>>x;
cout<<"你出";
display(x);
while(1)
{
if(y!=0)//防止无限循环
break;
}
cout<<"电脑出";
display(y);
int f=judge(x,y);
return f;
}


void Continue1(){
    int f;
    do{
         f=Play();}
while(f==tie);
if(f==win){
    cout<<"恭喜你战胜了电脑!!"<<endl;
}else
    cout<<"很遗憾,您输了"<<endl;
}

void Continue2(){
    int f;
    do{
        f=Play();}
while(f==tie);
if(f==win){
    Continue1();
}
else 
    cout<<"很遗憾,您输了"<<endl;}
void Continue3(){
int f;
    do{
        f=Play();}
while(f==tie);
if(f==lose){
    Continue1();
}
else 
    cout<<"恭喜你战胜了电脑!!"<<endl;}
void Continue4(){
    int flag1=2;
    int flag2=0;
    int f;
    do{
        f=Play();
        if(f==win){
        flag1=++flag1;}
        else if(f==lose){
        flag2=++flag2;}}
while(f==tie);
if(flag2==1){
    Continue4();
}
else 
    cout<<"恭喜你战胜了电脑!!"<<endl;
}
void Continue5(){
    int flag1=0;
    int flag2=2;
    int f;
    do{
        f=Play();
        if(f==win){
        flag1=++flag1;}
        else if(f==lose){
        flag2=++flag2;}}
while(f==tie);
if(flag1==1){
    Continue5();
}
else 
    cout<<"很遗憾,您输了"<<endl;}

void Set1(){
    int flag1=0;
    int flag2=0;
    int i;
    for(i=0;i<2;i++){
        int g=Play();
        if(g==win){
            flag1=++flag1;  
        }   

        else if(g==lose){
        flag2=++flag2;

        }
    }  
    if(flag1==2){
        cout<<"恭喜你战胜了电脑!!"<<endl;

    }else if(flag2==2){
    cout<<"很遗憾,您输了"<<endl;
    }else if(flag1==1&&flag2==1){
        Continue1();    }
    else if(flag1==1&&flag2==0){
    Continue3();}
    else if(flag1==0&&flag2==1){
    Continue2();}
    else
        Set1();

}

void Set2(){
    int flag1=0;
    int flag2=0;
    int i;
    for(i=0;i<3;i++){
        int g=Play();
        if(g==win){
            flag1=++flag1;  
    }   

        else if(g==lose){
        flag2=++flag2;

        }
    }
    if(flag1==3){
        cout<<"恭喜你战胜了电脑!!"<<endl;

    }else if(flag2==3){
    cout<<"很遗憾,您输了"<<endl;
    }else if(flag1==1&&flag2==2){
        Continue2();    }
    else if(flag1==2&&flag2==1){
    Continue3();}
    else if(flag1==1&&flag2==1){
        Set1();}
    else if(flag1==2&&flag2==0){
    Continue4();}
    else if(flag1==0&&flag2==2){
    Continue5();}
    else
        Set2();
}

int main()
{
    int a;
    cout<<"欢迎来到剪刀石头布游戏,按1选择三局两胜制,按2选择五局三胜制"<<endl;
    cin>>a;

        switch (a)//选择三局两胜或五局三胜
    {
    case 1:
        cout<<"您选择三局两胜"<<endl;
        cout<<"0代表出石头,1代表出剪刀,2代表出布"<<endl;
        Set1();
        break;
    case 2:
        cout<<"您选择五局三胜制"<<endl;
        cout<<"1代表出石头,2代表出剪刀,3代表出布"<<endl;
        Set2();
            break;
    }
        getchar();
        return 0;

}

图片说明

然后运行经常这样卡住,无论按什么键都没有反应,程序也不进行了,卡的时间随机,但全部都是在电脑准备出拳的时候卡住,请问是什么原因?

  • 写回答

1条回答 默认 最新

  • zhenmu 2020-05-07 17:52
    关注

    while(1)
    {
    if(y!=0)//防止无限循环
    break;
    }

    如果 这个 y为0不就一直卡住了吗? 这句话有啥意义?

    另外 没法调试的问题,是不是你用了release编译 而不是debug? 或者是vs环境有问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答