0

我正在做航空公司预订项目,我的要求是,当一个座位被占用时,它显示一个*,当它是空的时,它显示一个#。我想将数组设置为布尔值,所以如果它是 false 则为 a #,如果为 true 则为 a *。这行得通还是我走远了?有没有更简单的方法来做到这一点?

bool seatFirst[4][3];
if(seatFirst == true)
    cout << "*" << endl;
else
    cout << "#";
4

1 回答 1

2

那是行不通的,因为您正在测试数组本身,这将评估为真。您需要测试单个元素。也不需要检查 a booltrue你可以做if (theBool). Finally, you cannot appendendl ; to a string literal, you need to "stream" it with anoperator<<`。

在这里,使用三元运算符使代码更简洁:

std::cout << (seatFirst[i][j] ? "*" : "#") << std::endl;
于 2013-03-10T19:15:18.373 回答