代码不工作(尝试使用!=正确)

时间:2014-12-25 23:55:39

标签: c++

#include <iostream>
#include <string>

using namespace std;


int main ()
{
//Get Seed Color
string seedColor = "";
cout << "Enter Your Seed's Color: \n";
cin >> seedColor;
// Get Temp

if(seedColor != "red" || seedColor != "Red" && seedColor != "blue" || seedColor != "Blue")
    {
    cout << "Invalid Response. Please enter Red or Blue for the Color.\n";
    return 0;
    }

int temp = 0;
cout << "Enter Your Seed's Temperature: ";
cin >> temp;

 // if Red Seed
if(seedColor == "Red"|| seedColor == "red")
{


    //if temp >= 75
    if (temp >= 75)
    {

         // Get Soil Moisutre
        string soilMoisture = "";
        cout << "Enter Your Soil's Moisture (Wet or Dry): ";
        cin >> soilMoisture;

        // Soil Dry Output Dandelion
        if (soilMoisture == "Wet" || soilMoisture == "wet" )
        {
            cout << "A Sunflower will grow!\n";
        }

        if (soilMoisture == "Dry" || soilMoisture == "dry")
        {
            cout << "A Dandelion will grow!\n";
        }
    }


    else
    {
       // Otherwise Mushroom
       cout << "You will have a Mushroom!";
    }



}
// if Blue seed
if (seedColor == "Blue" || seedColor == "blue")
{


    //if temp b/t 60 && 70
    if (temp >= 60 && temp <= 70)
    {
        // Get Soil Moisutre
        string soilMoisture = "";
        cout << "Enter Your Soil's Moisture (Wet or Dry): ";
        cin >> soilMoisture;

        // Soil Dry Output Sunflower
        if (soilMoisture == "Dry" || soilMoisture == "dry")
        {
            cout << "A Sunflower will grow!";
        }
        // Soil Wet Output Dandelion
        if (soilMoisture == "Wet" || soilMoisture == "wet")
        {
            cout << "A Dandelion will grow! \n";
        }

    }
    else
    {
        // Otherwise Mushroom
        cout << "You will have a Mushroom! \n";
    }


}
    return 0;
}
嘿伙计们,所以我在尝试解决这个问题时遇到了一些麻烦。每当我运行此代码时,无论我是否放置&#34; Red&#34; &#34;蓝色&#34;甚至像香蕉&#34;香蕉&#34;它给了我&#34;无效的响应&#34;提示。任何想法如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

if(seedColor != "red" || seedColor != "Red" && seedColor != "blue" || seedColor != "Blue")

首先可能更容易为可接受的种子颜色重写if。我们知道什么是好的,对于许多程序员来说,&#34; good&#34;条件是最容易设想的:

if( seedColor == "red" || seedColor == "Red" || 
    seedColor == "blue" || seedColor == "Blue")
{
   // this is a good seed color
}
else
{
   // this is a bad seed color
}

现在,如果你真的想测试糟糕的种子颜色,那么只需要在整个条件下放置一个!就可以了:

if(!(seedColor == "red" || seedColor == "Red" || 
    seedColor == "blue" || seedColor == "Blue") )
{
   // this is a bad seed color
}
else
{
   // this is a good seed color
}

有一个名为Demorgan's Law的定理:http://en.wikipedia.org/wiki/De_Morgan%27s_laws

在此处应用它将产生以下结果:

if (seedColor != "red" && seedColor != "Red" && 
    seedColor != "blue" && seedColor != "Blue")
{
   // this is a bad seed color
}
else
{
   // this is a good seed color
}

换句话说,==转为!=||转为&&。基本上,根据您关于!=的问题,我们使用了3个步骤来提出所需的结果:

  1. 写好&#34;良好的条件&#34;
  2. 简单地通过not条件来描述不良条件。
  3. 应用Demorgan的法律来提出正确的!=用法。
  4. 但是,我建议另一种方法是编写一个返回&#34; ok&#34;如果种子颜色好:

    bool seedColorOk(const std::string& sColor)
    {
       return seedColor == "red" || seedColor == "Red" || 
            seedColor == "blue" || seedColor == "Blue";
    }
    //...
    if( !seedColorOk(seedColor))
    {
       // this is a bad seed color
    }
    else
    {
       // this is a good seed color
    }
    

    然后可以自定义该功能而不会破坏原始代码。

答案 1 :(得分:2)

如果seedColor是&#34;红色&#34;那么seedColor!=&#34; Red&#34;将是真的。

如果seedColor是&#34; Red&#34;那么seedColor!=&#34;红色&#34;将是真的。

如果seedColor是其他任何东西,那么seedColor!=&#34; Red&#34;和seedColor!=&#34;红色&#34;两者都是真的。

所以无论seedColor是什么,表达式seedColor!=&#34; red&#34; || seedColor!=&#34; Red&#34;将是真的。想想你应该如何结合两个子表达式来获得你想要的东西。

如前所述,&amp;&amp;优先级高于||,因此需要修复另一个问题。

答案 2 :(得分:0)

问题主要不在于!=关闭。在此学习逻辑运算符的优先级:http://www.learncpp.com/cpp-tutorial/36-logical-operators/:D 这是“正确”的方式。(如果这是你想要的结果)

#include <iostream>
#include <string>

using namespace std;


int main ()
{
//Get Seed Color
string seedColor = "";
cout << "Enter Your Seed's Color: \n";
cin >> seedColor;
// Get Temp

if((seedColor != "red" && seedColor != "Red") && (seedColor != "blue" || seedColor != "Blue"))
{
cout << "Invalid Response. Please enter Red or Blue for the Color.\n";
return 0;
}

int temp = 0;
cout << "Enter Your Seed's Temperature: ";
cin >> temp;

 // if Red Seed
if(seedColor == "Red"|| seedColor == "red")
{


//if temp >= 75
if (temp >= 75)
{

     // Get Soil Moisutre
    string soilMoisture = "";
    cout << "Enter Your Soil's Moisture (Wet or Dry): ";
    cin >> soilMoisture;

    // Soil Dry Output Dandelion
    if (soilMoisture == "Wet" || soilMoisture == "wet" )
    {
        cout << "A Sunflower will grow!\n";
    }

    if (soilMoisture == "Dry" || soilMoisture == "dry")
    {
        cout << "A Dandelion will grow!\n";
    }
}


else
{
   // Otherwise Mushroom
   cout << "You will have a Mushroom!";
}



}
// if Blue seed
if (seedColor == "Blue" || seedColor == "blue")
{


//if temp b/t 60 && 70
if (temp >= 60 && temp <= 70)
{
    // Get Soil Moisutre
    string soilMoisture = "";
    cout << "Enter Your Soil's Moisture (Wet or Dry): ";
    cin >> soilMoisture;

    // Soil Dry Output Sunflower
    if (soilMoisture == "Dry" || soilMoisture == "dry")
    {
        cout << "A Sunflower will grow!";
    }
    // Soil Wet Output Dandelion
    if (soilMoisture == "Wet" || soilMoisture == "wet")
    {
        cout << "A Dandelion will grow! \n";
    }

}
else
{
    // Otherwise Mushroom
    cout << "You will have a Mushroom! \n";
}


}
    return 0;
}