如何正确评估此if语句 [英] How do I get this if statement to properly evaluate

查看:101
本文介绍了如何正确评估此if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个涉及生日计划中的try-catch块的家庭作业。虽然我的大部分内容已经停止,但我仍然需要使用if语句,因此程序会在少于31天的时间内正确评估。



< pre lang =c ++> if ((month = 4 6 9 11 )&&(date> 30 ))
throw invalidDay();





每当我尝试运行它时,它会向我抛出invalidDay()。如果我从程序中删除它,该程序可以找到。我环顾四周但却找不到任何东西,所以我想知道是否有人可以帮助我?



我尝试了什么:



我试过删除程序中的代码,我尝试过使用||运算符,我试过限制if语句的大小(即:month = 4&& date> 30)

解决方案

你没有,因为有几个原因:

1)Equals是一个赋值运算符:你根本不应该在if语句中使用它,并且你不能为一个变量分配四个值。

2)即使您使用了比较运算符==,也可以根据四个值进行检查,它不是在此列表中运算符,而C ++没有运算符。

尝试:

  if ((month ==  4  || month ==  6  || month ==  9  ||月==  11 )&& date>  30 
{
...


I'm working on a homework assignment involving try-catch blocks in a birthday program. While I have most of it down I'm stuck at an if statement I need to use so the program evaluates properly for months that have less than 31 days.

if ((month = 4, 6, 9, 11) && (date > 30))
			throw invalidDay();



Whenever I try to run it, it throw's the invalidDay() at me. If I remove it from the program, the program works find. I looked around but couldn't really find anything so I was wondering if someone might be able to help me?

What I have tried:

I have tried deleting the code from the program, I have tried using the || operator, I have tried limiting the size of the if statement(ie: month = 4 && date > 30)

解决方案

You don't, for several reasons:
1) Equals is an assignment operator: you shouldn't use it in an if statement at all, and you can't assign four values to a single variable.
2) Even if you used the comparison operator "==", you can;t check it against four values, it isn't an "in this list" operator, and C++ doesn't have one.
Try:

if ((month == 4 || month == 6 || month == 9 || month == 11) && date > 30)
   {
   ...


这篇关于如何正确评估此if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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