将两个数相乘,然后添加另一个数字,结果为“0”。作为答案 [英] Multiplying two numbers, then adding another results in "0" as answer

查看:89
本文介绍了将两个数相乘,然后添加另一个数字,结果为“0”。作为答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将2个数字相乘,然后在该结果中添加第三个数字。
下面的
s1是一个十进制数,最多4位小数。

t1以下是十进制数字(钱)

t7以下是十进制数字,最多3位小数



将变量转换为数字,例如,我的计算应该如下所示:



(100.012 * 0.1700)+ $ 168.62应该总和,从而使tbTrkGalsPurch1:$ 185.62



显然我在某个我不熟悉的地方违反了规则。我在Debug或程序EXE中都没有收到任何错误消息。我的TRY条款没有收到任何错误消息。



 私人 < span class =code-keyword> Sub  calc()
Dim a,s1,t1,t7 作为 十进制

s1 = CDec (IIf (tbStateTax1.Text.Trim = ,0D,tbStateTax1.Text.Trim))
t1 = CDec (IIf(tbPumpCost1.Text.Trim = ,0D,tbPumpCost1.Text.Trim))
t7 = CDec (IIf(tbTrkGalsPurch1.Text.Trim = ,0D,tbTrkGalsPurch1.Text.Trim))

尝试
如果 t7> 0 AndAlso cbTermPurch1.Text.ToString = 然后
a =(t7 * s1)+ t1
tbTrkFuelPurch1。 Text = a.ToString( C2
Else
tbTrkFuelPurch1.Text = t1.ToString( C2
结束 如果
Catch ex As 异常
' 如果发生计算错误,请显示错误消息框
Dim frm As < span class =code-keyword>新 MeMsgCalcError(例如, 'Fuel plus Tax Stop 1'计算错误。& vbCrLf& 第479-490行
frm.Show()
结束 尝试
结束 Sub



在调试期间,我的值(t7,s1,t1)在执行时都显示正确的值对变量进行鼠标悬停但a值为0,这使我相信格式化或我尝试进行计算的方式存在问题。我怀疑格式化问题是问题。



我尝试过:



我试图将整个等式括起来:a =((t7 * s1)+ t1)

我试图在没有加法的情况下进行乘法计算:a =(t7 * s1)

我试图删除:AndAlso cbTermPurch1.Text.ToString =No部分的等式。

我试图改变目前的格式:

s1 = CDec(IIf(tbStateTax1.Text.Trim =,0D,tbStateTax1.Text.Trim))

我改为:

s1 = CDec(IIf(tbStateTax1.Text.Trim =,4D,tbStateTax1.Text.Trim))

解决方案

168.62应该总和,从而制作tbTrkGalsPurch1 :


185.62



显然我在某个我不熟悉的地方违反了规则。我在Debug或程序EXE中都没有收到任何错误消息。我的TRY条款没有收到任何错误消息。



 私人 < span class =code-keyword> Sub  calc()
Dim a,s1,t1,t7 作为 十进制

s1 = CDec (IIf (tbStateTax1.Text.Trim = ,0D,tbStateTax1.Text.Trim))
t1 = CDec (IIf(tbPumpCost1.Text.Trim = ,0D,tbPumpCost1.Text.Trim))
t7 = CDec (IIf(tbTrkGalsPurch1.Text.Trim = ,0D,tbTrkGalsPurch1.Text.Trim))

尝试
如果 t7> 0 AndAlso cbTermPurch1.Text.ToString = 然后
a =(t7 * s1)+ t1
tbTrkFuelPurch1。 Text = a.ToString( C2
Else
tbTrkFuelPurch1.Text = t1.ToString( C2
结束 如果
Catch ex As 异常
' 如果发生计算错误,请显示错误消息框
Dim frm As < span class =code-keyword>新 MeMsgCalcError(例如, 'Fuel plus Tax Stop 1'计算错误。& vbCrLf& 第479-490行
frm.Show()
结束 尝试
结束 Sub



在调试期间,我的值(t7,s1,t1)在执行时都显示正确的值对变量进行鼠标悬停但a值为0,这使我相信格式化或我尝试进行计算的方式存在问题。我怀疑格式化问题是问题。



我尝试过:



我试图将整个等式括起来:a =((t7 * s1)+ t1)

我试图在没有加法的情况下进行乘法计算:a =(t7 * s1)

我试图删除:AndAlso cbTermPurch1.Text.ToString =No部分的等式。

我试图改变目前的格式:

s1 = CDec(IIf(tbStateTax1.Text.Trim =,0D,tbStateTax1.Text.Trim))

我改为:

s1 = CDec(IIf(tbStateTax1.Text.Trim =,4D,tbStateTax1.Text.Trim))


对不起,我很难想你知道您声称的调试器。您无法解决此问题与调试器的知识相矛盾。

尝试使用调试器进行更改,看看出了什么问题。

 s1 =  0  1700  
t1 = 168 . 62
t7 = 100 012
a =(t7 * s1)+ t1

a = 123 45
tbTrkFuelPurch1.Text = a.ToString( C2



如果输出为0,则表示格式化问题。

如果 a 得到计算,它是从字符串问题的转换。



你自己没有做那些睾丸,看看问题在哪里是令人难以置信的。



[更新]

在您的问题中,Debug这个词是2次。如果你没有说你没有掌握调试器,那么每个人都会期望你这样做。

我的建议:尽快学习调试器。

- 在你的例程开始时设置一个断点。

- 打开局部变量窗口。

-execute一步一步你编程并看看哪一行被执行,看看变量是如何演变的。



在我的代码的第一部分,我在变量中强制有效值并计算 a 。看看 a 是否保持0不应该很复杂。

第二部分我设置 a ,看格式是否正常不应该很复杂。

如果你是这段代码的作者,你应该能够改变它以便做到测试


I am attempting to multiply 2 numbers together then add a third number to that result.
s1 below is a decimal number up to 4 decimal places.
t1 below is a decimal number (money)
t7 below is a decimal number up to 3 decimal places

Converting the variables to numbers, for an example, my calculation should look like this:

(100.012 * 0.1700) + $168.62 should sum to and thereby make tbTrkGalsPurch1: $185.62

Obviously I am breaking a rule somewhere I am not familiar with. I receive no error messages either in Debug or in the program EXE. I receive no error messages on my TRY clause.

Private Sub calc()
    Dim a, s1, t1, t7 As Decimal

    s1 = CDec(IIf(tbStateTax1.Text.Trim = "", 0D, tbStateTax1.Text.Trim))
    t1 = CDec(IIf(tbPumpCost1.Text.Trim = "", 0D, tbPumpCost1.Text.Trim))
    t7 = CDec(IIf(tbTrkGalsPurch1.Text.Trim = "", 0D, tbTrkGalsPurch1.Text.Trim))

    Try
        If t7 > 0 AndAlso cbTermPurch1.Text.ToString = "No" Then
            a = (t7 * s1) + t1
            tbTrkFuelPurch1.Text = a.ToString("C2")
        Else
            tbTrkFuelPurch1.Text = t1.ToString("C2")
        End If
    Catch ex As Exception
        'If a calculation error occurs, show Error message box
        Dim frm As New MeMsgCalcError(ex, "'Fuel plus Tax Stop 1' Calculation Error." & vbCrLf & "Lines 479-490")
        frm.Show()
    End Try
End Sub


During debug my values (t7, s1, t1) are all showing the proper values when doing a mouseover on the variable yet the 'a' value is 0 which leads me to believe there is something amiss either in the formatting or the way I am attempting to do the calculation. I suspect a formatting issue is the problem.

What I have tried:

I have attempted to bracket the entire equation: a = ((t7 * s1) + t1)
I have attempted to do the multiplication calculation without the addition: a = (t7 * s1)
I have attempted to remove the: AndAlso cbTermPurch1.Text.ToString = "No" portion of the equation.
I have attempted to change formatting where currently I have:
s1 = CDec(IIf(tbStateTax1.Text.Trim = "", 0D, tbStateTax1.Text.Trim))
I changed to:
s1 = CDec(IIf(tbStateTax1.Text.Trim = "", 4D, tbStateTax1.Text.Trim))

解决方案

168.62 should sum to and thereby make tbTrkGalsPurch1:


185.62

Obviously I am breaking a rule somewhere I am not familiar with. I receive no error messages either in Debug or in the program EXE. I receive no error messages on my TRY clause.

Private Sub calc()
    Dim a, s1, t1, t7 As Decimal

    s1 = CDec(IIf(tbStateTax1.Text.Trim = "", 0D, tbStateTax1.Text.Trim))
    t1 = CDec(IIf(tbPumpCost1.Text.Trim = "", 0D, tbPumpCost1.Text.Trim))
    t7 = CDec(IIf(tbTrkGalsPurch1.Text.Trim = "", 0D, tbTrkGalsPurch1.Text.Trim))

    Try
        If t7 > 0 AndAlso cbTermPurch1.Text.ToString = "No" Then
            a = (t7 * s1) + t1
            tbTrkFuelPurch1.Text = a.ToString("C2")
        Else
            tbTrkFuelPurch1.Text = t1.ToString("C2")
        End If
    Catch ex As Exception
        'If a calculation error occurs, show Error message box
        Dim frm As New MeMsgCalcError(ex, "'Fuel plus Tax Stop 1' Calculation Error." & vbCrLf & "Lines 479-490")
        frm.Show()
    End Try
End Sub


During debug my values (t7, s1, t1) are all showing the proper values when doing a mouseover on the variable yet the 'a' value is 0 which leads me to believe there is something amiss either in the formatting or the way I am attempting to do the calculation. I suspect a formatting issue is the problem.

What I have tried:

I have attempted to bracket the entire equation: a = ((t7 * s1) + t1)
I have attempted to do the multiplication calculation without the addition: a = (t7 * s1)
I have attempted to remove the: AndAlso cbTermPurch1.Text.ToString = "No" portion of the equation.
I have attempted to change formatting where currently I have:
s1 = CDec(IIf(tbStateTax1.Text.Trim = "", 0D, tbStateTax1.Text.Trim))
I changed to:
s1 = CDec(IIf(tbStateTax1.Text.Trim = "", 4D, tbStateTax1.Text.Trim))


Sorry, but it is difficult for me to think that you know the debugger as you claim. Your inability to solve this problem is contradictory with the knowledge of debugger.
Try those changes with debugger and see what go wrong.

s1 = 0.1700
t1 = 168.62
t7 = 100.012
a = (t7 * s1) + t1

a = 123.45
tbTrkFuelPurch1.Text = a.ToString("C2")


if output is 0, it is formatting problem.
if a get calculated, it is conversion from string problem.

It is incredible that you didn't do those testes by your own to see where is the problem.

[Update]
The word Debug is 2 times in your question. If you don't say that you don't master the debugger, everyone will expect that you do.
My advice: learn the debugger as soon as possible.
-set a breakpoint at beginning of your routine.
-open the local variables window.
-execute step by step you program ans see which line is executed and see how variables evolves.

In first part of my code, I force valid values in variables and compute a. Should not be complicated to see if a remain 0 or not.
Is second part I set a with a valid value, should not be complicated to see if formatting is ok or not.
if you are the author of this code, you should be able to change it in order to do the tests.


这篇关于将两个数相乘,然后添加另一个数字,结果为“0”。作为答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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