为什么当我输入正确的 switch 语句时被忽略
Why when i put the right input the switch statement get ignored
输出:
Select your sign Tom: [X] or [O]
Y
ERROR ENTER A DIFFERENT CHARACTER
X
Player 2 = O
但我想要这样:
Select your sign Tom: [X] or [O]
Y
ERROR ENTER A DIFFERENT CHARACTER
X
Player 1 = X
Player 2 = O
printf("Select your sign %s: [X] or [O]\n", PLAYER1);
scanf(" %c", &P1_SIGN);
do{
switch (P1_SIGN) { //entrata segni
case 'X':
puts("Player 1 = X\n");
P1_SIGN = 'X';
break;
case 'x':
puts("Player 1 = X\n");
P1_SIGN = 'X';
break;
case 'O':
puts("Player 1 = O\n");
P1_SIGN = 'O';
break;
case 'o':
puts("Player 1 = O\n");
P1_SIGN = 'O';
break;
default:
puts("ERROR ENTER A DIFFERENT CHARACTER");
scanf(" %c", &P1_SIGN);
break;
}} while(P1_SIGN != 'X' && P1_SIGN != 'x' && P1_SIGN != 'O' && P1_SIGN != 'o');
if (P1_SIGN == 'X' || P1_SIGN == 'x') {
puts("Player 2 = O\n");
} else {
puts("Player 2 = X\n");
}
你的 scanf()
位置不对。
第二个scanf()
将读取输入X
并且它会使do-while
循环的条件为假。因此,循环体不会再次 运行,因此不会打印 Player 1 = X
。
您应该删除两个 scanf()
,而不是在 do-while
循环内的 switch
语句之前添加一个 scanf()
。
do{
scanf(" %c", &P1_SIGN); // put scanf() here
switch (P1_SIGN) { //entrata segni
输出:
Select your sign Tom: [X] or [O]
Y
ERROR ENTER A DIFFERENT CHARACTER
X
Player 2 = O
但我想要这样:
Select your sign Tom: [X] or [O]
Y
ERROR ENTER A DIFFERENT CHARACTER
X
Player 1 = X
Player 2 = O
printf("Select your sign %s: [X] or [O]\n", PLAYER1);
scanf(" %c", &P1_SIGN);
do{
switch (P1_SIGN) { //entrata segni
case 'X':
puts("Player 1 = X\n");
P1_SIGN = 'X';
break;
case 'x':
puts("Player 1 = X\n");
P1_SIGN = 'X';
break;
case 'O':
puts("Player 1 = O\n");
P1_SIGN = 'O';
break;
case 'o':
puts("Player 1 = O\n");
P1_SIGN = 'O';
break;
default:
puts("ERROR ENTER A DIFFERENT CHARACTER");
scanf(" %c", &P1_SIGN);
break;
}} while(P1_SIGN != 'X' && P1_SIGN != 'x' && P1_SIGN != 'O' && P1_SIGN != 'o');
if (P1_SIGN == 'X' || P1_SIGN == 'x') {
puts("Player 2 = O\n");
} else {
puts("Player 2 = X\n");
}
你的 scanf()
位置不对。
第二个scanf()
将读取输入X
并且它会使do-while
循环的条件为假。因此,循环体不会再次 运行,因此不会打印 Player 1 = X
。
您应该删除两个 scanf()
,而不是在 do-while
循环内的 switch
语句之前添加一个 scanf()
。
do{
scanf(" %c", &P1_SIGN); // put scanf() here
switch (P1_SIGN) { //entrata segni