自学:初学者尝试制作温度转换器
我正在尝试构建一个温度转换器来帮助自己学习 C#。我只知道大部分基础知识,这就是我到目前为止所想到的。我所坚持的是获取用户输入的数字,并将其转换为用户之前输入的选择,即华氏度或摄氏度。再说一次,我只知道基础知识,但非常感谢您的帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What sort of temperature would you like to convert?");
string tempType = Console.ReadLine();
ConvertChoice(tempType.ToLower());
Console.WriteLine("Please enter a temperature to convert: ");
string temperatureString = Console.ReadLine();
int temperature = int.Parse(temperatureString);
Console.ReadLine();
}
static void ConvertChoice(string tempType)
{
switch (tempType)
{
case "farenheit":
Console.WriteLine("Farenheit it is!");
return;
case "celsius":
Console.WriteLine("Celsius it is!");
return;
default:
Console.WriteLine("Invalid type, please type farenheit or celsius.");
return;
}
}
}
}
I am trying to build a temperature converter to help myself learn C#. I only know most of the basics, and this is what I have come up with so far. What I am stuck on, is taking the number the user puts in, and converting it to the choice that the user previously entered, being farenheit or celsius. Again, I only know the basics, but help would very appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What sort of temperature would you like to convert?");
string tempType = Console.ReadLine();
ConvertChoice(tempType.ToLower());
Console.WriteLine("Please enter a temperature to convert: ");
string temperatureString = Console.ReadLine();
int temperature = int.Parse(temperatureString);
Console.ReadLine();
}
static void ConvertChoice(string tempType)
{
switch (tempType)
{
case "farenheit":
Console.WriteLine("Farenheit it is!");
return;
case "celsius":
Console.WriteLine("Celsius it is!");
return;
default:
Console.WriteLine("Invalid type, please type farenheit or celsius.");
return;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设您输入类似“摄氏度,20”的内容,意味着您想要将 20°C 转换为华氏度,您需要一些像这样的逻辑,
但不确定这是否能回答您的问题。
更好的方式是支持开尔文。将用户输入的输入温度转换为开尔文温度,然后将开尔文温度转换为用户想要的温度。然后,您可以支持与任何类型的单位之间的转换,而无需单独处理每种情况:
如果您没有看到优势,想象一下如何为 5 或 10 个不同的单位(而不是 2 个)进行编码。
Assuming you enter something like "Celsius, 20" meaning you want to convert 20ºC to Fahrenheit, you need some logic like this
Not sure if this answers your question, though.
A fancier way would be to support Kelvin as well. Convert the input temperature to Kelvin from whatever the user enters, and then convert Kelvin to whatever the user wants. Then you can support converting to/from any kind of units without having to handle each case individually:
If you don't see the advantage, imagine how would you code this for 5 or 10 different units instead of just 2.
这个怎么样?
}
How bout this?
}
使用对象方法....请原谅一些可能的语法/样式错误,通常我自己不使用 C#...
那么您的主类将类似于:
Using an object approach.... Forgive some likely syntax / style errors,normally don't use c# myself...
Then your main class would look something like:
你的程序有一些缺点;首先,您需要保存用户想要执行哪种类型的转换,以便当他/她输入需要转换的温度时,您可以实际执行它。由于您仅使用两种温度类型(华氏度和摄氏度(是的,好吧,谁使用 Réaumur 呢?)),您可以将用户选择存储为布尔值,指示是否选择了华氏度。您可能还想接受十进制数字。
因此,话虽如此,您可以通过以下方式更改程序以反映我的建议:
请注意,我已通过循环直到获得有效值来确保为温度类型和温度值输入正确的值。
我希望这可以引导您进一步自学的正确方向。如果您对上述内容有任何疑问,请随时询问。作为免责声明,我必须说我没有编译上面的代码,但我的心理语法检查器通常非常可靠;-)
Your program has a few shortcomings; first of all you need to save which type of conversion the user wants to perform, so that you can actually perform it when he/she has entered the temperature that needs to be converted. Since you are only operating with two temperature types (Fahrenheit and Celsius (yeah, well, who uses Réaumur anyway?)) you can store the user choice as a boolean that indicates whether or not Fahrenheit was chosen. You might also want to accept decimal numbers.
So, having said that, here's how you could alter your program to reflect my suggestions:
Note that I have made sure that correct values are entered for both temperature type and temperature value by looping until a valid value is obtained.
I hope this guides you in the right direction for further self-learning. Please don't hesitate to ask if you have any questions regarding the above. As a disclaimer I must say that I have not compiled the above code, but my mental syntax checker is usually pretty reliable ;-)
您已将他们的选择存储在 tempType 中。用那个。
只需从 main() 中调用此函数即可。
(注意:是的,我意识到这在功能上是有限的,并且假设只有两种可能的温标。OP说他正在学习编程,所以我用了最简单的例子)。
编辑
修正了我的算法。现在逻辑实际上按预期工作了。
You've got their choice stored in tempType. Use that.
Just call this function from your main().
(Note: Yes, I realize that this is limited in its functionality and assumes that there are only two possible temperature scales. The OP said he was learning programming, so I went with the simplest example possible).
EDIT
Fixed my algorithm. Now the logic actually works as intended.