带有布尔逻辑表达式的if-else语句 [英] If-else statement with boolean logical expression

查看:153
本文介绍了带有布尔逻辑表达式的if-else语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的作业编写了这段代码,但在输出中输入y或n后,我似乎根本没有收到任何消息。



我尝试了什么:



I wrote this code for my assignment but i don't seem to get any message at all after i put 'y' or 'n' in my output.

What I have tried:

if ((studentAnswer == 'Y') || (seniorCitizenAnswer == 'Y'))
	{
		cout << "Congratulations!" << endl;
		cout << "You qualify for discounted movie tickets." << endl;
	}
	else if ((studentAnswer == 'N') && (seniorCitizenAnswer == 'N'))
	{
		cout << "Sorry." << endl;
		cout << "You must be a student or a senior citizen to receive discounted movie tickets." << endl;
	}

	cout << endl << endl;

	return 0;

推荐答案

您检查一些用户输入Y和N,但用户输入可以是y或n,在这种情况下您的测试失败。

解决方案:

- 你需要强制用户输入大写字母。

- 加上'y'和'n'的检查。

- 如果用户输入则用大写测试;使用 toupper 功能。



如果用户输入只能是'Y'和'N',代码可以简化,因为如果iris不是一个,它会自动成为另一个。

You check some user input against 'Y' and 'N' but user input can be 'y' or 'n', in which case your test fail.
Solutions:
- you need to force user to input uppercase.
- add a check for 'y' and 'n' too.
- test with uppercase if user input; by using the toupper function.

If user input can only be 'Y' and 'N', your code can be simplified because if iris not one, it is automatically the other.
if ((studentAnswer == 'Y') || (seniorCitizenAnswer == 'Y'))
{
}
else if ((studentAnswer == 'N') && (seniorCitizenAnswer == 'N'))
{
}


这篇关于带有布尔逻辑表达式的if-else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆