在c++中用Switch语句编写程序,根据键盘输入数字,输出对应星期
时间: 2024-09-19 21:03:51 浏览: 60
在C++中,你可以使用switch语句来根据用户输入的数字来输出对应的星期。例如,1代表星期一,2代表星期二,以此类推直到7分别代表周日。以下是一个简单的例子:
```cpp
#include <iostream>
int main() {
int dayInput;
char daysOfWeek[8] = {'S', 'M', 'T', 'W', 'T', 'F', 'S', 'U'}; // S表示Sunday
std::cout << "Enter a number from 1 to 7 for the corresponding day of the week: ";
std::cin >> dayInput;
switch (dayInput) {
case 1:
std::cout << "You entered: " << daysOfWeek[0] << " (Saturday)" << std::endl;
break;
case 2:
std::cout << "You entered: " << daysOfWeek[1] << " (Monday)" << std::endl;
break;
// ... 添加更多的case for 其他数字
default:
std::cout << "Invalid input! Please enter a number between 1 and 7." << std::endl;
break;
}
return 0;
}
阅读全文