即使有 break 语句,代码也会执行所有情况
Code executes all cases even with break statement
这是一个旋转位的简单程序,switch case 用于定义用户在获得最高有效位或最低有效位后是要向左旋转还是向右旋转,具体取决于我在 side 循环中使用的选择开关和我使用了相同的值,无论我输入什么,它都会执行所有情况。
#include <stdio.h>
#define SIZE sizeof(number)*8
void main()
{
int number, rotate, i, SB, numberr;
char choice;
puts("simple program to rotate bits of a number");
printf("If you want right rotation type 'R'\n");
printf("enter your number : ");
scanf("%d", &number);
printf("Enter how much time you want to rotate : ");
scanf("%d", &rotate);
getchar();
printf("IF you want left rotation type 'L' : ");
scanf("%c", &choice);
switch(choice)
{
case 'l' :
case 'L' :
SB = (1<<(SIZE-1))&1;
for(i=0;i<rotate;i++)
{
numberr = (number << 1) | SB;
printf("\n%d after being rotated %d times became %d\n\n", number, rotate, numberr);
}
break;
case 'r' :
case 'R' :
SB = number & 1;
for(i=0;i<rotate;i++)
{
numberr = (number >> 1) | SB;
printf("\n%d after being rotated %d time became %d\n\n", number, rotate, numberr);
}
break;
default :
printf("please chose either R or L\n\n");
}
}
我已经完善了您的代码,这可能会有所帮助。
在发布问题之前,您可能希望改进代码的语法。
#include <stdio.h>
#define SIZE 32
int main() {
int number, rotate, i, number1;
char choice;
int output;
puts("simple program to rotate bits of a number");
printf("enter your number : ");
scanf("%d", &number);
printf("Enter how much time you want to rotate : ");
scanf("%d", &rotate);
printf("If you want right rotation type 'R'\n");
printf("IF you want left rotation type 'L' : ");
scanf(" %c", &choice);
switch(choice) {
case 'l' :
case 'L' :
output = (number << rotate) | (number >> (SIZE - rotate));
printf("%d after rotating %d times became %d,",number,rotate,output);
break;
case 'r' :
case 'R' :
output = (number >> rotate) | (number << (SIZE - rotate));
printf("%d after rotating %d times became %d,",number,rotate,output);
break;
default :
printf("Please choose either R or L\n\n");
}
return 0;
}
输出令人困惑,因为来自每个 case
的消息的措辞相同。当我在消息中添加L
和R
时,显示只执行了一个case
。
问题是输出在循环内,需要移到循环外,例如
case 'l' :
case 'L' :
SB = (1<<(SIZE-1))&1;
for(i=0;i<rotate;i++)
{
numberr = (number << 1) | SB;
}
printf("L %d after being rotated %d times became %d\n\n", number, rotate, numberr);
break;
这是一个旋转位的简单程序,switch case 用于定义用户在获得最高有效位或最低有效位后是要向左旋转还是向右旋转,具体取决于我在 side 循环中使用的选择开关和我使用了相同的值,无论我输入什么,它都会执行所有情况。
#include <stdio.h>
#define SIZE sizeof(number)*8
void main()
{
int number, rotate, i, SB, numberr;
char choice;
puts("simple program to rotate bits of a number");
printf("If you want right rotation type 'R'\n");
printf("enter your number : ");
scanf("%d", &number);
printf("Enter how much time you want to rotate : ");
scanf("%d", &rotate);
getchar();
printf("IF you want left rotation type 'L' : ");
scanf("%c", &choice);
switch(choice)
{
case 'l' :
case 'L' :
SB = (1<<(SIZE-1))&1;
for(i=0;i<rotate;i++)
{
numberr = (number << 1) | SB;
printf("\n%d after being rotated %d times became %d\n\n", number, rotate, numberr);
}
break;
case 'r' :
case 'R' :
SB = number & 1;
for(i=0;i<rotate;i++)
{
numberr = (number >> 1) | SB;
printf("\n%d after being rotated %d time became %d\n\n", number, rotate, numberr);
}
break;
default :
printf("please chose either R or L\n\n");
}
}
我已经完善了您的代码,这可能会有所帮助。 在发布问题之前,您可能希望改进代码的语法。
#include <stdio.h>
#define SIZE 32
int main() {
int number, rotate, i, number1;
char choice;
int output;
puts("simple program to rotate bits of a number");
printf("enter your number : ");
scanf("%d", &number);
printf("Enter how much time you want to rotate : ");
scanf("%d", &rotate);
printf("If you want right rotation type 'R'\n");
printf("IF you want left rotation type 'L' : ");
scanf(" %c", &choice);
switch(choice) {
case 'l' :
case 'L' :
output = (number << rotate) | (number >> (SIZE - rotate));
printf("%d after rotating %d times became %d,",number,rotate,output);
break;
case 'r' :
case 'R' :
output = (number >> rotate) | (number << (SIZE - rotate));
printf("%d after rotating %d times became %d,",number,rotate,output);
break;
default :
printf("Please choose either R or L\n\n");
}
return 0;
}
输出令人困惑,因为来自每个 case
的消息的措辞相同。当我在消息中添加L
和R
时,显示只执行了一个case
。
问题是输出在循环内,需要移到循环外,例如
case 'l' :
case 'L' :
SB = (1<<(SIZE-1))&1;
for(i=0;i<rotate;i++)
{
numberr = (number << 1) | SB;
}
printf("L %d after being rotated %d times became %d\n\n", number, rotate, numberr);
break;