第一个双码mp / h km / h [英] First double code mp/h km/h

查看:132
本文介绍了第一个双码mp / h km / h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本框TX_MPH中加载速度,现在我想要KMH转换器的值为KMH。

我有以下代码由来自互联网的来源组成,但我该怎么写这个代码,TX_KMH.Text填充正确的值,形成km1。



I load speed in a textbox "TX_MPH", Now I want the value of MPH Converter for KMH.
I have the following code composed of a source from the internet but how do I write this code that the TX_KMH.Text get filled with the correct value form kilometers1.

private void Speed {
TX_MPH.Text = GPS.GPRMC.Speed.ToString() + " mp/h";

double miles1 = 200;
double kilometers1 = ConvertMilesToKilometers(200);
Console.WriteLine("{0} = {1}", miles1, kilometers1);


TX_KMH.Text = ??
}

public static double ConvertMilesToKilometers(double miles)
{
return miles * 1.609344;
}





我的尝试:





What I have tried:

double miles1 = GPS.GPRMC.Speed.ToString();
double kilometers1 = ConvertMilesToKilometers(GPS.GPRMC.Speed.ToString());
Console.WriteLine("{0} = {1}", miles1, kilometers1);



但这不是正确的方法..我收到错误.. :(


But this is not the correct method .. I get errors back.. :(

推荐答案

TX_KMH.Text = kilometers1.ToString();


引用:

但是如何编写这段代码,TX_KMH.Text填充正确的值,形成km1。

but how do I write this code that the TX_KMH.Text get filled with the correct value form kilometers1.

因为你是新手编程,你需要做的第一件事就是能够了解每行代码的作用。你问的是如何将返回值,公里数放到文本框中。



看看

Since you are new to programming the first thing you need to do is be able to understand what each line of code is doing. You are asking how to get the return value, the kilometers, into a textbox.

Look at

double kilometers1 = ConvertMilesToKilometers(200);



这是将数字200传递给函数以转换为千米。请注意,它将值存储在名为km1的变量中。



所以,把它放进去在文本框中的值是简单的:


That is passing the number 200 into the function to convert to kilometers. Notice that it is storing the value in a variable named kilometers1.

So, to put that value into the textbox simple do:

TX_KMH.Text = kilometers1.ToString(); 





就这么简单。



现在,你还提到你得到错误。首先要知道的是,错误几乎总能告诉你究竟出了什么问题。其次,如果你要求别人帮忙,那就告诉他们你得到了什么错误以及代码行引起了什么错误,这样我们就不用猜了。



看看你的代码,我可以告诉你,double km1 = ConvertMilesToKilometers(GPS.GPRMC.Speed.ToString());会导致错误。为什么?因为ConvertMilesToKilometers函数接受一个参数但希望它采用双精度格式。当你调用那个函数时,你在Speed上调用.ToString(),所以当函数想要一个Double时你试图传递一个String。



我不知道是什么类型速度是因为我看不到你的代码。如果它已经是double类型,那么只需删除.ToString()。如果它的类型不是双倍,那么你需要转换它。我会留给你学习。



It's that simple.

Now, you also mention you get errors. First thing to know is that the error will almost always tell you exactly what is wrong. Second, if you are asking people for help then tell them what error you got and what line of code caused that error so that we don't have to guess.

Looking at your code I can tell you that double kilometers1 = ConvertMilesToKilometers(GPS.GPRMC.Speed.ToString()); will cause an error. Why? Because the function ConvertMilesToKilometers takes a parameter but expects it in the double format. When you called that function you call .ToString() on Speed so you are trying to pass a String when the function wants a Double.

I don't know what type Speed is because I can't see your code. If it is already of type double then just remove .ToString(). If it's type is not double then you need to convert it. I'll leave that to you for learning.


这篇关于第一个双码mp / h km / h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆