百木生 2020-10-23 10:56 采纳率: 50%
浏览 116
已采纳

大佬求救,为什么Switch会跳不出去,这是什么错误啊

图片说明

#include<iostream>
#include<vector>
#include<string>
using namespace std;
void display(char n) {
    char a[4][6];
    switch (n-'0') {
    case 0:
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                if (j == 2 && ((i != 1) && (i != 5))) a[i][j] = '.';
                else a[i][j] = 'X';
            }
        }
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                cout << a[i][j];
            }
            cout << '\n';
        }
        break;
    case 1:
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                if (j == 3) a[i][j] = 'X';
                else a[i][j] = '.';
            }
        }break;
    case 2:
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                if ((i == 2 && j != 3) || (i == 4 && j != 1)) a[i][j] = '.';
                else a[i][j] = 'X';
            }
        }
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                cout << a[i][j];
            }
            cout << '\n';
        }
        break;
    case 3:
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                if ((i == 2 && j != 3) || (i == 4 && j != 3)) a[i][j] = '.';
                else a[i][j] = 'X';
            }
        }
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                cout << a[i][j];
            }
            cout << '\n';
        }
        break;
    case 4:
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                if ((j == 2 && i != 3) || (i == 4 && j != 3) || (i == 5) && (j != 3)) a[i][j] = '.';
                else a[i][j] = 'X';
            }
        }
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                cout << a[i][j];
            }
            cout << '\n';
        }
        break;
    case 5:
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                if ((i == 2 && j != 1) && (i == 4 && j != 3)) a[i][j] = '.';
                else a[i][j] = 'X';
            }
        }
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                cout << a[i][j];
            }
            cout << '\n';
        }
        break;
    case 6:
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                if ((i == 2 && j != 1) && (i == 4 && j == 2)) a[i][j] = '.';
                else a[i][j] = 'X';
            }
        }
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                cout << a[i][j];
            }
            cout << '\n';
        }
        break;
    case 7:
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                if (i == 1 || j == 3) a[i][j] = 'X';
                else a[i][j] = '.';
            }
        }
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                cout << a[i][j];
            }
            cout << '\n';
        }
        break;
    case 8:
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                if ((i == 2 && j == 2) || (i == 4 && j == 2)) a[i][j] = '.';
                else a[i][j] = 'X';
            }
        }
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                cout << a[i][j];
            }
            cout << '\n';
        }
        break;
    case 9:
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                if ((i == 2 && j == 2) || (i == 4 && j != 3)) a[i][j] = '.';
                else a[i][j] = 'X';
            }
        }
        for (int i = 1; i < 6; i++) {
            for (int j = 1; j < 4; j++) {
                cout << a[i][j];
            }
            cout << '\n';
        }
        break;
    }
}
int main() {
    int x;
    string s;
    cin >> x;
    cin >> s;
    for (int i = 0; i < s.size(); i++) {
        display(s[i]);
    }
    return 0;
}
  • 写回答

1条回答 默认 最新

  • Onvejon 2020-10-23 12:00
    关注

    看你 a 的定义 以及后面 i j 的范围,很明显 i应该小于 4的,你的 i <6 数组访问越界了

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

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络