gpt4 book ai didi

c++ - 无法执行else语句,它会给出错误

转载 作者:行者123 更新时间:2023-12-03 07:08:53 25 4
gpt4 key购买 nike

**
我想打印b和c之间的每个整数(n),以便如果n> 9则执行else语句并以偶数或奇数的形式打印出每个整数。if语句在c和b变量的以下条件满足时执行是正确的,所以如果我输入c和b都<9,那没有问题。
**

#include <iostream>
#include <cstdio>
using namespace std;

int main() {

int b;
int c;

string x[9] = {"one","two","three","four","five","six","seven","eight","nine"};

cin >> c;
cin >> b;

for (int r=0 ; c <= b ; c+1) {

c=c+1+r;

if (1<= c <= 9) {
cout << x[c-2] << endl;
} else {
if (c%2 != 0) {
cout << "odd" << endl;
} else {
cout << "even" << endl;
}
}
c;
}

return 0;
}

最佳答案

条件1<= c <= 9无法按预期工作。1<= c <= 9被视为(1 <= c) <= 9
此处的1 <= c将被视为01,因此条件(1 <= c) <= 9始终为true。
您应该改为使用1 <= c && c <= 9

关于c++ - 无法执行else语句,它会给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64226730/

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