如何创建if / else语句来完成此任务

时间:2016-04-08 05:05:16

标签: c++

如果用户要为两个房子输入相同的基本价格和平方英尺,我怎么能创建另一个if / else语句来输出两种房屋类型?感谢所有帮助。

的#include     使用namespace std;

int main()
{
    int baseColonial;
    int baseSplit;
    int baseSingle;
    int sqftColonial;
    int sqftSplit;
    int sqftSingle;
    int priceColonial;
    int priceSplit;
    int priceSingle;

    cout << "Please enter the base price of the Colonial home: ";
    cin >> baseColonial;
    cout << "Now please enter the finished area in square feet: ";
    cin >> sqftColonial;

    cout << "Please enter the base price of the Split-entry home: ";
    cin >> baseSplit;
    cout << "Now please enter the finished area in square feet: ";
    cin >> sqftSplit;

    cout << "Please enter the base price of the Single-story home: ";
    cin >> baseSingle;
    cout << "Now please enter the finished area in square feet: ";
    cin >> sqftSingle;

    priceColonial = baseColonial / sqftColonial;
    priceSplit = baseSplit / sqftSplit;
    priceSingle = baseSingle / sqftSingle;


    if ((priceColonial <= priceSplit) && (priceColonial <= priceSingle))
    {
        cout << endl << "The Colonial house is the cheapest." << endl;
    }
    else if ((priceSplit <= priceColonial) && (priceColonial >= priceSingle))
    {
        cout << endl << "The split-entry house is the cheapest." << endl;
    }
    else if ((priceSingle <= priceSplit) && (priceSplit >= priceColonial))
    {
        cout << endl << "The single-story house if the cheapest." << endl;
    }
    return 0;

我只是尝试将此代码用于一个场景,其中所有3个都是每平方英尺相同的价格,但它无法正常运行。我错过了什么?

否则if((priceSingle == priceSplit)&amp;&amp;(priceSingle == priceColonial))     {         cout&lt;&lt; endl&lt;&lt; “所有三种房屋模型的平均价格都相同。” &LT;&LT; ENDL;     }

3 个答案:

答案 0 :(得分:0)

在下面检查 - 如果梯子:

if (priceColonial < priceSplit) {
    if (priceColonial < priceSingle) {
        cout << endl << "The Colonial house is the cheapest." << endl;
    }
    else if(priceColonial == priceSingle) {
        cout << endl << "The Colonial and Single-story houses are cheaper." << endl;
    }
    else {
        cout << endl << "The Single-entry house is the cheapest." << endl;
    }
}
else if(priceColonial == priceSplit) {
    if (priceColonial < priceSingle) {
        cout << endl << "The Colonial and Split-entry houses are cheaper." << endl;
    }
    else if(priceColonial == priceSingle) {
        cout << endl << "All are of same price." << endl;
    }
    else {
        cout << endl << "The Single-entry house is the cheapest." << endl;
    }
}
else {
    if (priceSplit < priceSingle) {
        cout << endl << "The Split-entry house is the cheapest." << endl;
    }
    else if(priceSplit == priceSingle) {
        cout << endl << "The Split-entry and Single-story houses are cheaper." << endl;
    }
    else {
        cout << endl << "The Single-entry house is the cheapest." << endl;
    }
}

答案 1 :(得分:0)

您需要将if / else if语句中的&gt; =和&lt; =比较更改为&gt;或者&lt;为了最后的其他声明工作。否则,当所有房屋价格相同时,将满足第一个if条件。也就是说,例如,如果所有房屋的价格都是50,那么因为您的第一个条件指定了(priceColonial &lt; = priceSplit)&amp;&amp; (priceColonial &lt; = priceSingle)这个条件将成立,因为你也在这种情况下检查是否相等。

你可以这样:

if ((priceColonial < priceSplit) && (priceColonial < priceSingle))
{
    cout << endl << "The Colonial house is the cheapest." << endl;
}
else if ((priceSplit < priceColonial) && (priceColonial > priceSingle))
{
    cout << endl << "The split-entry house is the cheapest." << endl;
}
else if ((priceSingle < priceSplit) && (priceSplit > priceColonial))
{
    cout << endl << "The single-story house if the cheapest." << endl;
}
else if ((priceSingle == priceSplit) && (priceSingle==priceColonial)) 
{ 
    cout << endl << "All three house models have the same price per     square foot." << endl; 
}

答案 2 :(得分:0)

只有不满足第一个else if中的条件时,才会执行if块。通常,仅凭一条if / else链,或者仅凭一个实际上乞求杀手错字的荒谬的猛chain链,就无法解决此类问题。 (另外,假设您然后需要添加第四种类型:尽管实际上对于许多类型而言,解决问题显然很简单,但实际上却变得难以管理。)

仅指示您要去的方向,您可能会想要这样的东西:

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

using namespace std;

namespace {

const int HOUSE_TYPE_COUNT = 3;
const char* const HOUSE_TYPE_NAMES[HOUSE_TYPE_COUNT] = {"Colonial", "Split-entry", "Single-story"};

struct Entry {
    const char* const name;
    int basePrice;
    int areaSqFt;
    int price;

    Entry(const char * const name_) : name(name_) {}
};

Entry inputEntry(const char* const name) {
    Entry result(name);
    cout << "Please enter the base price of the " << name << " home: ";
    cin >> result.basePrice;
    cout << "Now please enter the finished area in square feet: ";
    cin >> result.areaSqFt;
    result.price = result.basePrice / result.areaSqFt;
    return result;
}

bool cheaper(Entry& left, Entry& right) {
    return left.price < right.price;
}

}

int main() {
    vector<Entry> entries;

    // User enters information
    for (int i = 0; i < HOUSE_TYPE_COUNT; ++i) {
        entries.push_back(inputEntry(HOUSE_TYPE_NAMES[i])); 
    }

    // Find cheapest price
    int cheapest = min_element(entries.begin(), entries.end(), cheaper)->price;

    // Output all types corresponding to cheapest price
    for (auto it = entries.begin(); it != entries.end(); ++it) {
        if (it->price == cheapest) {
            cout << "The " << it->name << " house is the cheapest." << endl;
        }
    }
    return 0;
}

如果您想将一行中的类型,可以做类似的事情

#include <sstream>

// ...

ostringstream answer;
answer << "The cheapest type(s): ";
const char* separator = "";
for (auto it = entries.begin(); it != entries.end(); ++it) {
    if (it->price == cheapest) {
        answer << separator << it->name;
        separator = ", ";
    }
}
cout << answer.str() << endl;

(免责声明:上面的代码在许多方面都是草率的。显然,它应该检查用户输入,不使用纯C字符,不使用整数除法,可能还有更多其他功能。这只是为了提供关于您将要使用的方法。)