switch语句输入英文单词
时间: 2023-12-22 15:26:32 浏览: 30
以下是使用switch语句在MATLAB中输入英文单词并输出对应中文的示例代码:
```matlab
day = input('请输入英文星期单词:', 's');
switch lower(day)
case 'monday'
disp('星期一');
case 'tuesday'
disp('星期二');
case 'wednesday'
disp('星期三');
case 'thursday'
disp('星期四');
case 'friday'
disp('星期五');
case 'saturday'
disp('星期六');
case 'sunday'
disp('星期天');
otherwise
disp('输入错误,请重新输入!');
end
```
在这个示例中,我们首先使用`input`函数从用户处获取输入的英文星期单词,并将其存储在变量`day`中。然后,我们使用`switch`语句根据输入的单词进行匹配,并输出对应的中文。注意,我们使用`lower`函数将输入的单词转换为小写字母,以便与`case`语句中的字符串进行匹配。
相关问题
c语言switch语句输入英文单词
C语言的switch语句是一种多分支选择结构,可以根据不同的条件执行不同的代码块。在输入英文单词时,我们可以使用switch语句来判断输入的单词是哪一个,并执行相应的代码块。具体实现如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char word[20];
printf("请输入一个英文单词:");
scanf("%s", word);
switch (word[0]) {
case 'a':
printf("apple\n");
break;
case 'b':
printf("banana\n");
break;
case 'c':
printf("cat\n");
break;
// 其他字母的判断
default:
printf("未知单词\n");
break;
}
return 0;
}
```
在上面的代码中,我们首先定义了一个字符数组`word`来存储输入的单词,然后使用`scanf`函数来读取用户输入的单词。接着,我们使用switch语句来判断输入的单词的首字母是哪一个,并执行相应的代码块。如果输入的单词不在我们的判断范围内,则输出“未知单词”。
用C编写一个程序,要求用户输入一个两位数然后显示该数的英文单词,用switch语句
Sure, here's the code:
```c
#include <stdio.h>
int main() {
int num;
printf("Please enter a two-digit number: ");
scanf("%d", &num);
switch(num) {
case 10:
printf("Ten\n");
break;
case 11:
printf("Eleven\n");
break;
case 12:
printf("Twelve\n");
break;
case 13:
printf("Thirteen\n");
break;
case 14:
printf("Fourteen\n");
break;
case 15:
printf("Fifteen\n");
break;
case 16:
printf("Sixteen\n");
break;
case 17:
printf("Seventeen\n");
break;
case 18:
printf("Eighteen\n");
break;
case 19:
printf("Nineteen\n");
break;
default:
switch(num / 10) {
case 2:
printf("Twenty ");
break;
case 3:
printf("Thirty ");
break;
case 4:
printf("Forty ");
break;
case 5:
printf("Fifty ");
break;
case 6:
printf("Sixty ");
break;
case 7:
printf("Seventy ");
break;
case 8:
printf("Eighty ");
break;
case 9:
printf("Ninety ");
break;
}
switch(num % 10) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
}
}
return 0;
}
```
Hope this helps!