Visual Basic温度转换器

时间:2011-08-25 05:39:24

标签: asp.net vb.net

我是Visual Basic的新手,我在Visual Studio 2010中创建一个aspx温度转换器应用程序,用户可以在文本框中输入数字,通过下拉列表框选择温度类型,然后选择温度到将其转换为单选按钮列表。我遇到的问题是,当我尝试转换某些东西时,我收到了一个错误。我收到错误“输入字符串格式不正确”和“从字符串转换”F“到类型'布尔'无效。”我已经尝试过嵌套的if..elseif..endif语句,但是当我这样做时它只是转换第一个if语句而没有别的。这是我为转换编写的代码。任何有关这方面的帮助将不胜感激。谢谢。

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    If (DropDownList1.SelectedValue = "F" & RadioButtonList1.SelectedValue = "F") Then
        Label1.Text = TextBox1.Text & "Fahrenheit=" & TextBox1.Text & "Fahrenheit"

    ElseIf (DropDownList1.SelectedValue = "F" & RadioButtonList1.SelectedValue = "C") Then
        Label1.Text = TextBox1.Text & "Fahrenheit=" & (TextBox1.Text * 1.8) + 32 & " Celsius"

    ElseIf (DropDownList1.SelectedValue = "F" & RadioButtonList1.SelectedValue = "K") Then
        Label1.Text = TextBox1.Text & "Fahrenheit=" & ((5 / 9) * (TextBox1.Text - 32) + 273) & " Kelvin"

    ElseIf (DropDownList1.SelectedValue = "C" & RadioButtonList1.SelectedValue = "C") Then
        Label1.Text = TextBox1.Text & "Celsius=" & TextBox1.Text & "Celsius"

    ElseIf (DropDownList1.SelectedValue = "C" & RadioButtonList1.SelectedValue = "F") Then
        Label1.Text = TextBox1.Text & " Celsius = " & (TextBox1.Text - 32) / 1.8 & " Fahrenheit"

    ElseIf (DropDownList1.SelectedValue = "C" & RadioButtonList1.SelectedValue = "K") Then
        Label1.Text = TextBox1.Text & " Celsius = " & (TextBox1.Text + 273) & " Kelvin"

    ElseIf (DropDownList1.SelectedValue = "K" & RadioButtonList1.SelectedValue = "K") Then
        Label1.Text = TextBox1.Text & "Kelvin=" & TextBox1.Text & "Kelvin"

    ElseIf (DropDownList1.SelectedValue = "K" & RadioButtonList1.SelectedValue = "F") Then
        Label1.Text = TextBox1.Text & "Kelvin=" & ((TextBox1.Text - 273) * 1.8) + 32 & "Fahrenheit"

    ElseIf (DropDownList1.SelectedValue = "K" & RadioButtonList1.SelectedValue = "C") Then
        Label1.Text = TextBox1.Text & "Kelvin=" & (TextBox1.Text - 237) & "Celsius"



    End If



End Sub

1 个答案:

答案 0 :(得分:1)

我认为这两个错误是:

  • 您在&条件下使用if - 这应该是and
  • 您正在尝试直接对字符串执行算术 - 您需要先将其转换为例如到双CDbl(TextBox1.Text)

那说可能有一定的余地可以重构一下:你可以这样做只计算温度数,然后在底部组装字符串,再次单独测试下拉列表,以便每个“Kelvin =”字符串只有一个副本等。