gpt4 book ai didi

c++ - 如何在 C++ 的 switch 语句中执行 if 条件

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:21:07 26 4
gpt4 key购买 nike

我有这个代码:

#include <iostream>

using namespace std;

int main() {
int square; char state;
cout<<"Write a numbber"; cin>>square;
square *= square;
cout<<square;
switch(square) {
case 1: state = 'h';
case 3: state = 'm';
case 7: state = 'j';
case (square > 10): state = 'u'; // I try this, but not works
}
return 0;
}

我想知道如何在 c++ 中的开关内部创建条件。

最佳答案

case 后面的表达式必须是编译时常量。因此,您可能不会使用您正在尝试的东西。

将其更改为 default:,然后使用 if

default:
if (square > 10)
state = 'u';

关于c++ - 如何在 C++ 的 switch 语句中执行 if 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49137485/

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