简体   繁体   English

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

[英]How to create an if/else statement to accomplish this

How could I create another if/else statement to output two house types if a user were to enter the same base price and square footage for two houses?? 如果用户要为两个房屋输入相同的基本价格和平方英尺,我如何创建另一个if / else语句来输出两种房屋类型? All help is appreciated. 感谢所有帮助。

#include using namespace std; #include使用命名空间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;

I just tried to use this code for a scenario where all 3 are the same price per sq ft but it's not functioning correctly. 我只是尝试将这种代码用于所有3个都是相同的每平方英尺价格,但无法正常运行的情况。 What am I missing? 我想念什么?

else if ((priceSingle == priceSplit) && (priceSingle == priceColonial)) { cout << endl << "All three house models have the same price per square foot." 否则if((priceSingle == priceSplit)&&(priceSingle == priceColonial)){cout << endl <<“所有三个房屋模型的每平方英尺价格都相同。” << endl; << endl; } }

Check below else-if ladder: 检查以下别的梯子:

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;
    }
}

You need to change the >= and <= comparison in your if/else if statements to just > or < for the last else statement to work. 您需要将if / else if语句中的> =和<=比较更改为仅>或<,以使最后的else语句起作用。 Otherwise the first if condition will be met when all the homes have equal price. 否则,当所有房屋的价格均等时,将满足第一个条件。 That is, if for example if all homes have a price of 50 then since you your first condition specifies (priceColonial <= priceSplit) && (priceColonial <= priceSingle) this condition will be true since you are also checking for equality in this condition. 也就是说,例如,如果所有房屋的价格均为50,则由于您的第一个条件指定了(priceColonial <= priceSplit)&&(priceColonial <= priceSingle),因此该条件成立,因为您还要在这种情况下检查是否均等。

you could have something like this: 你可能会有这样的事情:

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; 
}

The else if block will only be executed if the condition in the first if wasn't met. else if块将仅在不满足第一个if条件的情况下执行。 You generally cannot solve this kind of problem with a single if/else chain, or only with an absurdly mammoth one that's practically begging for killer typos. 通常,仅凭一条if / else链,或者仅凭一个实际上乞求杀手错字的荒谬的猛chain链,就无法解决此类问题。 (Also, imagine that you then need to add a fourth type: it becomes practically unmanageable - even though the problem is clearly simple to solve for many types.) (另外,假设您然后需要添加第四种类型:尽管实际上对于许多类型而言,解决问题显然也很简单,但是它实际上变得难以管理。)

Just to indicate the direction you want to go, you'll probably want something like this: 只是为了表明您想去的方向,您可能会想要这样的东西:

#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;
}

If you want the types on one line, you can do things like 如果您想将一行中的类型,可以执行以下操作

#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;

(Disclaimer: The code above is sloppy in a number of ways. Obviously it should check user inputs, not use plain C chars, not use integer division, and probably a lot more. It's just to give a general idea about the approach you'll want to use.) (免责声明:上面的代码在许多方面都是草率的。显然,它应该检查用户输入,不使用纯C字符,不使用整数除法,甚至可能更多。这只是为了大致了解您使用的方法我想使用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM