为什么这个测试总是返回假
Why does this test always return false?
请忽略此代码的上下文。这个想法是 名为 shop()
的函数将采用两个参数 (money_in_pocket
,age
(并确定这些值是否会 让他们进入劳力士专卖店。然而,即使当 参数满足 if 语句的要求 shop()
,程序继续输出"离开!- 意思是离开商店。
您可能已经注意到,我是该语言的新手,所以任何 帮助将不胜感激。
我尝试使参数远大于if
声明要求他们成为。这输出了"离开!",所以我尝试了不符合要求的参数,并且 显示相同的输出...
#include <iostream>
using namespace std;
class rolex{
public:
bool shop(int x, int y){
if((x >= 5000 && y>= 18)||(x>=5000 && y<18)){
bool enterence = true;
}else{
bool enterence = false;
};
return enterence;
}
private:
bool enterence;
};
int main()
{
rolex objj;
if( objj.shop(5000, 18) == true){
cout<<"you may enter"<<endl;
}else{
cout<<"LEAVE"<<endl;
}
return 0;
}
在 if 语句中
if((x >= 5000 && y>= 18)||(x>=5000 && y<18)){
bool enterence = true;
}else{
bool enterence = false;
};
您声明了两个局部变量,这两个变量在退出 if 语句后将不处于活动状态。
因此,数据成员rolex::enterence
未初始化,并且具有不确定的值。
更改 if 语句,例如
if((x >= 5000 && y>= 18)||(x>=5000 && y<18)){
enterence = true;
}else{
enterence = false;
};
考虑到 if 语句中的条件等效
于 if( x >= 5000 ){
你可以只写而不是 if 语句
enterence = x >= 5000;
或
rolex::enterence = x >= 5000;
以下是程序的简单编辑,可以按预期工作:
#include <iostream>
using namespace std;
class rolex {
private:
bool entrance;
public:
bool shop(int x, int y) {
if(x >= 5000 && y>= 18) {
entrance = true;
} else {
entrance = false;
}
return entrance;
}
};
int main() {
rolex obj;
if(obj.shop(5000, 18) == true) {
cout << "you may enter" << endl;
} else {
cout << "LEAVE" << endl;
}
return 0;
}
相关文章:
- 我是否正确测试了返回值优化?
- 使用 Google 基准测试时返回值会发生什么情况?
- 测试用例状态为失败,但返回的值为 true
- 谷歌测试:如何返回成功结果?
- 为什么生成文件返回 g++: 错误: h 文件或目录 make: *** [生成文件:12: 测试] 错误 1?
- 谷歌嘲笑!我的测试出错.默认情况下,呼叫将返回调用
- 为什么这个测试总是返回假
- 单位测试QT中的非返回功能
- 使用C ,使用SFINAE测试静态成员的存在,返回错误的值
- 如何测试仅返回系统值的API
- Google Mock - 使用 CoTaskMemFree 时,测试返回没有结果
- 如何在调试版本中测试断言,但在发布中返回值
- 二进制搜索测试柜不返回值
- 返回语句中的条件测试
- 显式布尔操作员 - 无法返回,测试,初始化bool
- 将函数中的值返回到由 while 循环测试的值.C++
- 检查两个数组中的相似元素,为某些测试返回正确的值,为其他测试返回不正确的值
- 为什么我的线段交叉测试不返回 true?
- 为什么 WinDbg 在测试内存泄漏程序时总是返回相同的"!heap -s"信息?
- 为什么一位数组返回零?合并排序测试