You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

投注算法程序终端输入无响应问题求助

问题修复:游戏模式选择输入1首次无响应的问题

核心问题原因

你的代码中,游戏模式选择的scanf语句存在格式错误:

scanf("%d\n", &Game_Type);

scanf的格式字符串里不需要手动添加\n——scanf会自动跳过输入中的空白字符(包括换行、空格、制表符)。当你在格式字符串里写\n时,scanf会持续等待,直到读取到非空白字符才会结束输入流程。这就导致你第一次输入1并回车后,程序还卡在等待状态,第二次输入1回车时才会触发后续逻辑。

修复方案

直接修改这行scanf,去掉格式字符串里的\n:

scanf("%d", &Game_Type);

额外的代码优化建议

  1. 随机数种子初始化问题
    你在Random_Number函数中每次调用都执行srand(time(NULL)),短时间内多次调用该函数时,time(NULL)返回的时间戳相同,会导致生成重复的随机数。应该把srand放在main函数开头,只初始化一次:

    int main(){
        srand(time(NULL)); // 移到这里,仅初始化一次
        int Game_Type,Choice;
        // ... 其余代码
    }
    
  2. 函数返回值不匹配
    Single_Game函数声明的返回值类型是double,但函数内部没有return语句,这会导致未定义行为。可以将函数改为void类型(因为你只是打印结果,没有需要返回的数值):

    void Single_Game(double Balance,double Bet,double Chance,double Multiplier){
        // ... 函数内部代码不变
    }
    

修改后的完整代码

#include<stdio.h>
#include<stdlib.h>
#include <time.h>

double Random_Number() {
    double ran_num;
    
    ran_num = rand() / (double)RAND_MAX * 100.0;
    return ran_num;
}

void Single_Game(double Balance,double Bet,double Chance,double Multiplier){
    Balance-=Bet;

    double Random= Random_Number();

    if (Random<=Chance)
    {
        Bet*=Multiplier;
        Balance+=Bet;
        printf("HERZLICHEN GLUECKWUNSCH!\nDu hast: %.2f Euro gewonnen !\nEs wurde %.2f gewuerfelt\nDein Neues Guthaben ist %.2f Euro\n", Bet,Random,Balance);
    }else printf("VERLOREN!\nEs wurde eine %.2f gewuerfelt.\n",Random);
}

/*double Multible_Games(double Balance,double Bet,double Chance_Selection){
        printf("Die Funktion ist noch nicht implementiert");
}*/

int main(){
    srand(time(NULL));
    int Game_Type,Choice;
    double Balance,Bet;

    printf("Wie dick ist der Geldbeutel? [Euro]\n");
    scanf("%lf", &Balance);

    printf("Wieviel willst du setzen ? [Euro]\n");
    scanf("%lf",&Bet);

    while (Bet>Balance)
    {
        printf("Zu kleiner Geldbeutel. Bitte die Wette anpassen:\n");
        scanf("%lf",&Bet);
    }

    printf("Mit welcher Chance willst du spielen ?\n----------------------------------------------------------------------------\n|  Chance  |   10%%  |  23%%  |  30%%  |  47,5%%  |  63,3%%  |   72%%  |   94%%   |\n----------------------------------------------------------------------------\n|  Payout  |  x9,5  |  x4   |  x3   |   x2    |  x1,5   |  x1,3  |  x1,01  |\n----------------------------------------------------------------------------\n|  Waehle  |    1   |   2   |   3   |    4    |    5    |    6   |    7    |\n----------------------------------------------------------------------------\n");
    scanf("%d",&Choice);

    float Chances [] ={10.0, 23.0, 30.0, 47.5, 63.33, 72.0, 91.0};
    float multipliers []= {9.5, 4.0, 3.0, 2.0, 1.5, 1.3, 1.01};

    float Chance = Chances[Choice-1];
    float Multiplier = multipliers[Choice-1];

    if (Choice>=1 && Choice<=7)
    {
        printf("Dein moeglicher Gewinn liegt bei %.2f Euro\n", Bet * Multiplier);
    } else printf("Die Eingabe wurde nicht erkannt. Bitte neu starten.\n");


    printf("Welcher Modus ?\n-------------------------------------\n|       Modus1       |    Modus2     |\n-------------------------------------\n|  Einmaliges Spiel  |  Automatisch  |\n-------------------------------------\n\n");
    scanf("%d", &Game_Type);
    while (Game_Type != 1 && Game_Type != 2)
    {
        printf("Die Eingabe wurde nicht erkannt, bitte erneut eingeben:\n");
        scanf("%d", &Game_Type);
    }
    if (Game_Type==1)
    {
        Single_Game(Balance, Bet, Chance, Multiplier);
    } 

    return 0;
}

内容的提问来源于stack exchange,提问作者Zerionic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 01:42:20