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

控制台无法显示ASCII 1-31字符,显示矩形问号求解决方案

解决控制台无法正常显示ASCII 1-31控制字符的问题

嘿,我来帮你搞定这个问题!控制台显示带问号的矩形,核心原因是Windows默认控制台的代码页(比如OEM 936中文代码页)不支持直接渲染ASCII 1-31的控制字符——这类字符原本是用来做设备控制的,不是为可视化设计的。不过如果确实需要让它们显示出图形,有两个实用的解决办法:

方法一:切换到支持控制字符图形的代码页

Windows的IBM 437代码页(美国英语OEM代码页)里,ASCII 1-31的控制字符对应了专门的图形符号。你只需要在代码开头添加一行切换代码页的指令:

// 切换到IBM 437代码页,让控制字符显示对应图形
SetConsoleOutputCP(437);

方法二:手动映射控制字符到可显示符号

如果不想修改系统代码页,可以自己写一个映射函数,把ASCII 1-31的字符转换成你想要的可显示图形(比如用ASCII 176-223的方块、线条符号):

char getDisplayChar(int ascii) {
    if (ascii >= 1 && ascii <= 31) {
        // 自定义映射,把控制字符转成可显示的图形符号
        return 176 + ascii;
    }
    return (char)ascii;
}

之后输出字符时调用这个函数即可。


修改后的完整示例代码(补全了老虎机逻辑)

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

int main() {
    // 切换到支持控制字符图形的代码页
    SetConsoleOutputCP(437);
    
    const int startingMoney = 500;
    const int turnCost = 50;
    const int doubleBonus = 60;
    const int tripleBonus = 100; // 修正了原代码里的拼写错误tripleBobus
    int money = startingMoney;
    char panel0 = 'X';
    char panel1 = 'X';
    char panel2 = 'X';
    srand(time(NULL));
    
    do {
        printf("\n当前余额: %d | 每轮花费: %d\n", money, turnCost);
        printf("按任意键开始本轮,按Q退出游戏...\n");
        char input = _getch();
        if (input == 'Q' || input == 'q') break;
        
        if (money < turnCost) {
            printf("余额不足,无法继续游戏!\n");
            break;
        }
        money -= turnCost;
        
        // 模拟老虎机转动,生成ASCII 1-31的字符
        panel0 = rand() % 31 + 1;
        panel1 = rand() % 31 + 1;
        panel2 = rand() % 31 + 1;
        printf("转动结果: %c | %c | %c\n", panel0, panel1, panel2);
        
        // 结算奖励
        if (panel0 == panel1 && panel1 == panel2) {
            money += tripleBonus;
            printf("🎉 恭喜中三倍奖励!余额+%d\n", tripleBonus);
        } else if (panel0 == panel1 || panel1 == panel2 || panel0 == panel2) {
            money += doubleBonus;
            printf("🎊 恭喜中双倍奖励!余额+%d\n", doubleBonus);
        } else {
            printf("😔 未中奖,继续加油!\n");
        }
        
    } while (1);
    
    printf("\n游戏结束!最终余额: %d\n", money);
    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:18:44