qt5写一个switch例子
时间: 2023-07-18 19:03:27 浏览: 148
程序举例switch语句
下面是一个在Qt5中使用switch语句的例子:
```cpp
#include <QCoreApplication>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int num = 3;
switch(num)
{
case 1:
cout << "Number is 1." << endl;
break;
case 2:
cout << "Number is 2." << endl;
break;
case 3:
cout << "Number is 3." << endl;
break;
default:
cout << "Number is not 1, 2, or 3." << endl;
break;
}
return a.exec();
}
```
此程序将输出“Number is 3.”,因为num被设置为3,并在switch语句中匹配到了case 3。如果num被设置为1或2,则会输出相应的消息。如果num不匹配任何case,则会输出默认消息。
阅读全文