vb.net Visual Basic 中的华氏度到摄氏度转换器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25824219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fahrenheit To Celsius Converter in Visual Basic
提问by Feyisayo Sonubi
I'm trying to write a program in vb where a user is asked to type in a value in either the Fahrenheit Textbox or the Celsius Textbox. I want to use just ONEbutton to perform the calculations and two textboxes to display the outputs but I'm not sure I understand what's happening. The number I type into the Textbox isn't what's been calculated.
我正在尝试用 vb 编写一个程序,要求用户在华氏文本框或摄氏文本框中输入一个值。我只想使用一个按钮来执行计算和两个文本框来显示输出,但我不确定我是否理解发生了什么。我在文本框中输入的数字不是计算出来的数字。
Here's the code:
这是代码:
Private Sub convertButton_Click(sender As Object, e As EventArgs) Handles convertButton.Click
Dim FahrenheitValue, CelsiusValue As Double
FahrenheitValue = Val(fahrenheitBox.Text)
CelsiusValue = Val(celsiusBox.Text)
FahrenheitValue = (9 / 5) * (CelsiusValue + 32)
CelsiusValue = (5 / 9) * (FahrenheitValue - 32)
celsiusBox.Text = CelsiusValue
fahrenheitBox.Text = FahrenheitValue
End Sub
I'm trying my best not to create different buttons for the calculations. How do I make the Boxes accept and calculate the right values typed into the box?
我正在尽力不为计算创建不同的按钮。如何让 Boxes 接受并计算输入到框中的正确值?
回答by mitchellJ
One major problem is here:
一个主要问题在这里:
FahrenheitValue = (9 / 5) * (CelsiusValue + 32)
CelsiusValue = (5 / 9) * (FahrenheitValue - 32)
Aside from the math being a little bit off you're changing the value right before using it again.
除了数学有点偏离之外,您还可以在再次使用之前更改该值。
I.E. I enter 0 in celsius:
IE 我在摄氏度中输入 0:
- FV = (9/5) * 0 + 32
- FV = (9/5) * 0 + 32
FV now equals 32
FV 现在等于 32
- CV = (5/9) * 32 - 32 == -14.22
- 简历 = (5/9) * 32 - 32 == -14.22
Try This:
尝试这个:
Dim ResultFV As Double = (CelsiusValue * (5 / 9) + 32)
Dim ResultCV As Double = (FahrenheitValue - 32) * (9 / 5)
Additionally it would be smart to clear the textbox values after you take them.
此外,在获取文本框值后清除它们将是明智之举。
Edit
编辑
The other comments are also correct that an additional problem is you're not setting which computation needs to be done.
其他评论也是正确的,另一个问题是您没有设置需要完成的计算。
Try:
尝试:
Public Class Form1
Dim celsiusActive As Boolean
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FahrenheitValue As Double = 0
Dim CelsiusValue As Double = 0
FahrenheitValue = Val(fahrenheitBox.Text)
CelsiusValue = Val(celsiusBox.Text)
Dim ResultFV As Double = (CelsiusValue * (5 / 9) + 32)
Dim ResultCV As Double = (FahrenheitValue - 32) * (9 / 5)
If celsiusActive Then
fahrenheitBox.Text = ResultFV
Else
celsiusBox.Text = ResultCV
End If
End Sub
Private Sub celsiusBox_TextChanged(sender As Object, e As EventArgs) Handles celsiusBox.TextChanged
celsiusActive = True
End Sub
Private Sub fahrenheitBox_TextChanged(sender As Object, e As EventArgs) Handles fahrenheitBox.TextChanged
celsiusActive = False
End Sub
End Class
回答by orca
if I understand it correctly, the celsius value would be nothing if the user enters the value in the Fahrenheit textbox. That way, your FahrenheitValue will always be calculated as (9/5)*(0+32). If it's the other way around, it should work. I think you would need an check where the user entered the value and depending on that, you perform the corresponding calculation.
如果我理解正确,如果用户在华氏度文本框中输入值,则摄氏度值将是零。这样,您的 FahrenheitValue 将始终计算为 (9/5)*(0+32)。如果是相反的方式,它应该可以工作。我认为您需要检查用户输入值的位置,并根据该值执行相应的计算。
The code would be sth like this:
代码将是这样的:
if fahrenheitBox.Text IS Nothing then
CelsiusValue = Val(celsiusBox.Text)
FahrenheitValue = (9 / 5) * (CelsiusValue + 32)
else
die