【问题标题】:Visual Studio Express 2010: Percentage Calculation (Calculator Function)Visual Studio Express 2010:百分比计算(计算器函数)
【发布时间】:2016-04-21 15:48:38
【问题描述】:

我正在为学校作业创建计算器。在其中,我必须包含一个百分比按钮,其中第一个输入 (Num1) 是百分比,然后是 % 按钮,然后是我们将计算百分比的第二个输入 (Num2),最后是等于按钮。

因此,例如,如果我说 25 % 100 = ,我希望显示的值是 25 %

但是,我的值一直返回 0 %,我不知道如何在特定情况下对操作进行编码。

我的百分比按钮的代码如下所示:

If txtDisplay.Text.Length <> 0 Then
      If Operation = String.Empty Then
          Num1 = CType(txtDisplay.Text, Double)
          txtDisplay.Text = "0"
      Else CalculateTotals()
      End If
      Operation = "Percent"
      Point = False
End If

这是我的计算函数的样子(不包括所有其他计算):

Function CalculateTotals()
    Num2 = CType(txtDisplay.Text, Double)
    Select Case Operation
            Case "Percent"
            Num1 /= 100 * Num2 & " %"
    End Select
    txtDisplay.Text = CType(Num1, String)
    Point = False
End Function

这将返回错误“从字符串“10000 %”到类型“Double”的转换无效。

如果有人能提出可能的解决方案,我们将不胜感激。

【问题讨论】:

    标签: visual-studio-2010


    【解决方案1】:

    不得不稍微改变一下,但我自己找到了答案,只需将其编码到百分比按钮本身。

    Private Sub Percent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercent.Click
        '##########################
        '# Percent
        '##########################
        'Percentage calculation, example: 500 * 10% = 50
        Num2 = CType(txtDisplay.Text, Double)
        txtDisplay.Text = Val(Num1 * (Num2 / 100))
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2013-10-27
      • 1970-01-01
      • 2011-06-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      相关资源
      最近更新 更多