【问题标题】:I tried to run this code but it keeps giving an output of else condition我试图运行这段代码,但它不断给出 else 条件的输出
【发布时间】:2022-06-22 05:34:19
【问题描述】:

字符串"Fahrenheit" 应该给出第一个if 语句的输出,但它却给出了else 语句的输出。

#include <iostream>

using namespace std;

class Temperature {
public:
    int temp;
    string unit;

    Temperature(int atemp, string aunit) {
        atemp = temp;
        aunit = unit;
    }

    void to_fahrenheit() {
        if (unit == "Fahrenheit") {
            cout << ((temp*1.8) + 32) << " Fahrenheit";
        } else if (unit == "Celsius") {
            cout << ((temp-32)*5/9) << " Celsius";
        } else {
            cout << "Converts only Fahrenheit to Celsius or vice versa.";
        }
    }
};

int main()  {
    Temperature temp1 (10,"Fahrenheit");
    temp1.to_fahrenheit();
    return 0;
}

【问题讨论】:

  • 在构造函数中应该是temp = atemp;而不是其他方式。
  • 并使用初始化列表
  • 您没有保存参数。此外,您正在进行双重转换 - F 应该产生 C ,反之亦然。 F 不应该产生 F。
  • 我尝试了@kotatsuyaki 评论的更改,这就是问题所在。谢谢你:)
  • 是的,我错过了那个。感谢您指出这一点! @杯子

标签: c++


【解决方案1】:

你的作业弄错了

Temperature(int atemp, string aunit)    {
    atemp = temp;
    aunit = unit;
}

应该是

Temperature(int atemp, string aunit)    {
   temp = atemp;
   unit = aunit;
}

这是逻辑错误而不是语法错误。

编写此代码的最佳方式是使用初始化列表

Temperature(int atemp, string aunit) : temp(atemp), unit(aunit) {
}

这样就不可能犯你所犯的错误。

【讨论】:

  • 谢谢!我不知道初始化列表,下次我会使用它。
【解决方案2】:

这里的问题是正确分配变量。

 Temperature(int atemp, string aunit)    {
temp = atemp;
unit = aunit;
}

在 C++ 中,= 运算符遵循从右到左的赋值。

【讨论】:

  • 好的,我已经更改了我的代码并且它有效。谢谢你:)
【解决方案3】:

您的构造函数实现错误,您应该将输入参数分配给类成员变量,而不是其他方式:

Temperature(int atemp, string aunit) 
   : temp{atemp}
   , unit{aunit} 
{
}

【讨论】:

  • 好的,我已经按照你说的做了修改,并且成功了。谢谢你:)
【解决方案4】:

您的代码在这一行有误。

atemp = temp;
aunit = unit;

必须是:

temp = atemp;
unit = aunit;

谢谢

【讨论】:

  • 我更改了代码,确实是问题所在。谢谢你:)
【解决方案5】:
#include <iostream>
using namespace std;

class Temperature   {
public:
    int temp;
    string unit;
    Temperature(int atemp, string aunit)    {
    //atemp = temp;
    //aunit = unit;
// change the assignment you will get your desired output
    temp = atemp;
    unit = aunit;
    }
    void to_fahrenheit()    {
        if  (unit == "Fahrenheit")   {
            cout << ((temp*1.8) + 32) << " Fahrenheit";
        } else if (unit == "Celsius") {
            cout << ((temp-32)*5/9) << " Celsius";
        } else  {
            cout << "Converts only Fahrenheit to Celsius or vice versa.";
        }
    }
};

int main()  {
    Temperature temp1 (10,"Fahrenheit");
    temp1.to_fahrenheit();
    return 0;
}

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案6】:

应为公共变量分配一个值,该值作为构造函数中的参数传递。所以,在温度构造函数中:

temp = atemp and unit = aunit

最终代码:

#include <iostream>
using namespace std;

class Temperature
{
public:
int temp;
string unit;
Temperature(int atemp, string aunit)
{
    temp = atemp;
    unit = aunit;
}
void to_fahrenheit()
{
    if (unit == "Fahrenheit")
    {
        cout << ((temp * 1.8) + 32) << " Fahrenheit";
    }
    else if (unit == "Celsius")
    {
        cout << ((temp - 32) * 5 / 9) << " Celsius";
    }
    else
    {
        cout << "Converts only Fahrenheit to Celsius or vice versa.";
    }
}
};

int main()
{
Temperature temp1(10, "Fahrenheit");
temp1.to_fahrenheit();
return 0;
}

【讨论】:

  • 谢谢!我理解了这个错误,非常感谢你的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 2016-03-27
  • 2022-12-31
相关资源
最近更新 更多