【发布时间】:2018-04-25 02:02:20
【问题描述】:
我不喜欢有很多 if 语句。无论如何,是否有一个 If 语句允许 int 等于多个数字,然后在输入其中任何一个数字时执行该语句?
#include <iostream>
using namespace std;
int main()
{
int year;
cin>>year;
//Rat
if (year==2008)
cout<<"The year "<< year <<" is the year of the Rat";
if (year==1996)
cout<<"The year "<< year <<" is the year of the Rat";
if (year==1984)
cout<<"The year "<< year <<" is the year of the Rat";
if (year==1972)
cout<<"The year "<< year <<" is the year of the Rat";
//Error message
if (year<1964)
cout<<"Please enter a valid number.";
if (year>2018)
cout<<"Please enter a valid number.";
return 0;
}
【问题讨论】:
-
if (year == 2006 || year == 1994 || ...) -
也可以使用算术:
if (year % 12 == 2) -
可能是开关/机箱?
-
与编程无关,但说 1994 年是鼠年是错误的。不仅鼠年是 1996 年而不是 1994 年,而且中国的新年通常要到 1 月下旬至 2 月中旬,所以与上一年相比有整整一个月左右的时间。
标签: c++ if-statement