如何解决请回答我的代码无法正常工作侦听器不起作用
#include <iostream>
#include <conio.h>
int main()
{
int C; //let C be Celsius
int F; //let F be Fahrenheit
cout << "Enter the temperature in Celsius: ";
cin >> C;
cout << "Enter the temperature in Fahrenheit: ";
cin >> F;
fahrenheit = (C * 9 / 5) + 32
celsius = (F - 32) * 5 / 9
cout << "The computed Fahrenheit value from temperature in Celsius: " << fahrenheit << endl;
cout << "The computed Celsius value from temperature in Fahrenheit: " << celcius << endl;
return 0;
getch();
}
解决方法
您真的需要说“不起作用”的意思。这段代码有很多问题。
但是看代码,主要问题似乎是您正在使用整数除法。
9/5
的值为1
,而不是1.8
。在C ++中,当您将一个整数除以另一个整数时,结果总是总是另一个整数。相反,如果您要小数,则应使用9.0/5.0
。
在C ++中,始终重要的是为变量选择正确的类型。温度是连续变化的量,因此对温度使用整数是错误的。改用float
或double
。
您未能声明两个变量celcius
和fahrenheit
。
您在某些陈述的结尾还缺少分号。
cin
名称空间中存在的标准库对象,例如cout
,std
等,应使用限定名称,std::cin
等进行引用
最后,如果您要使用getch
暂停程序,则必须在之前您的return
之前,
这是您的程序,其中有这些更正内容。
#include <iostream>
#include <conio.h>
int main()
{
double C; //let C be Celsius
double F; //let F be Fahrenheit
std::cout << "Enter the temperature in Celsius: ";
std::cin >> C;
std::cout << "Enter the temperature in Fahrenheit: ";
std::cin >> F;
double fahrenheit = (C * 9.0 / 5.0) + 32.0;
double celsius = (F - 32.0) * 5.0 / 9.0;
std::cout << "The computed Fahrenheit value from temperature in Celsius: " << fahrenheit << std::endl;
std::cout << "The computed Celsius value from temperature in Fahrenheit: " << celcius << std::endl;
getch();
return 0;
}
如您所见,您的代码存在很多问题。您无法通过大致正确的方式进行编程。它必须完全正确,否则将无法正常工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。