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

大佬求救,为什么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;

}

回答

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

以上是 大佬求救,为什么Switch会跳不出去,这是什么错误啊 的全部内容, 来源链接: utcz.com/a/63101.html

回到顶部