【问题标题】:multiple if statements cpp [duplicate]多个if语句cpp [重复]
【发布时间】: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


【解决方案1】:

鼠年每 12 年发生一次,因此您可以使用:

if(year % 12 == 2) {
    cout << "The year " << year << " is the year of the Rat" << endl;
}

您只需要确保在此之前进行范围检查 (if (year&lt;1964)...),因为这不会关心日期的早晚。


然而,一个快速的谷歌搜索显示鼠年实际上是 1972、1984、1996...所以虽然我上面的代码是你发布的代码的有效缩写,但正确的代码应该是:

if(year % 12 == 4) {
    cout << "The year " << year << " is the year of the Rat" << endl;
}

如果我们想推广到所有生肖动物,我们可以使用 mod 和 std::vector 轻松做到这一点:

std::vector<std::string> zodiac_animals = {"Monkey", "Rooster", "Dog", "Pig", "Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat"};
cout << "The year " << year << " is the year of the " << zodiac_animals.at(year%12) << endl;

【讨论】:

  • 或者您可以将文本更改为“狗年”。同样正确。今年(2018 年)确实是狗年。
【解决方案2】:

您可以使用逻辑运算符:

if (year == 2008 || year == 1996 || year == 1984 || year == 1972)

您也可以使用 switch/case 并在案例之间不使用 break 时利用失败行为:

switch (year) {
case 2008:
case 1996:
case 1984:
case 1972:
    cout >> "The year " << year << " is the year of the Rat" << endl;
    break;
    ...
}

但最简单的方法是利用十二生肖中的 12 年周期,用算术和向量来做到这一点,因此您不必明确列出年份(可能会弄错年份,和你一样)。

const std::vector<const std::string> zodiac {"Monkey", "Rooster", "Dog", "Pig", "Rat", "Ox", "Tiger", "Rabbit", "Dragon"< "Snake", "Horse", "Goat"};
cin >> year;
cout << "The year " << year << " is the year of the " << zodiac[year % zodiac.size()] << '\n';

【讨论】:

  • 首先你的评论比我快 40 秒,然后你的向量实现比我快几分钟。你在我的脑海里!自从你第一次发布你的向量实现后,我应该编辑我的向量实现吗?我不希望它看起来像是在接受你的想法......
  • 别担心。如果我们同时编辑,它们都是有效的。
【解决方案3】:

如果您要检查的数据集是有限的,并且您确实希望最大限度地减少各种分支结构,您始终可以使用容器和 std::algorithm,例如 find。这是一个例子(你可以阅读更多关于它here):

#include <iostream>
#include <algorithm>
#include <vector>

int main ()
{
  int myints[] = { 10, 20, 30, 40 }; // finite set of values
  std::vector<int> myvector (myints, myints + 4);
  std::vector<int>::iterator it;

  it = find (myvector.begin(), myvector.end(), 30);
  if (it != myvector.end()) // Only one if
    std::cout << "Element found in myvector: " << *it << '\n';
  else
    std::cout << "Element not found in myvector\n";

  return 0;
}

这将始终归结为对std::find(或任何其他算法,取决于您的情况)和一个if 的调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    相关资源
    最近更新 更多