gpt4 book ai didi

c - 将 Switch 语句转换为 if/then 和 if/else 语句

转载 作者:太空宇宙 更新时间:2023-11-04 07:52:02 25 4
gpt4 key购买 nike

我想把这个 switch 语句变成 if/else 和 if/then 语句。我的 Switch 语句是:

char option;
printf("Choose your option : ");
scanf("%c",&option);

switch(option){
case 'a':
case 'A': a = 20 + 10 ;
printf("Addition process result:%d",a);
break;
case 'b':
case 'B': a = 20 - 10 ;
printf("Subtraction process result:%d",a);
break;
case 'c':
case 'C': a = 20 * 10 ;
printf("Multiplication process result:%d",a);
break;
case 'd':
case 'D': a = 20 + 10 ;
printf("Division process result:%d",a);
break;
default:
printf("Invalid option");
}

最佳答案

像这样:

#include <stdio.h>

int main() {
char option;
printf("Choose your option : ");
scanf("%c",&option);

if (option == 'a' || option == 'A') {
int a = 20 + 10;
printf("Addition process result:%d", a);
} else if (option == 'b' || option == 'A') {
int a = 20 - 10;
printf("Subtraction process result:%d", a);
} else if (option == 'c' || option == 'C') {
int a = 20 * 10;
printf("Multiplication process result:%d", a);
} else if (option == 'd' || option == 'D') {
int a = 20 + 10;
printf("Division process result:%d", a);
} else {
printf("Invalid option");
}
return 0;
}

关于c - 将 Switch 语句转换为 if/then 和 if/else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53212453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com