如何让我的sqrt按钮和百分比按钮在我的计算器上工作 [英] How do i get my sqrt button and percentage button to work on my calculator
问题描述
除了sqrt和百分比之外,我还有其他所有操作按钮。我对加法,减法等做了同样的事情,它似乎没有用。
这是我的全局值
I got every other operation button to work besides sqrt, and percentage. I've done the same thing for addition, subtraction, etc. and it doesn't seem to work.
here's my globals
Public Class Form1
Private first As Double
Private second As Double
Private oper As String
Private negpos As Boolean
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
first = txtoutput.Text
txtoutput.Clear()
oper = "+"
End Sub
Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click
second = txtoutput.Text
If (oper = "+") Then
txtoutput.Text = first + second
Else
If (oper = "-") Then
txtoutput.Text = first - second
Else
If (oper = "*") Then
txtoutput.Text = first * second
Else
If (oper = "/") Then
txtoutput.Text = first / second
Else
If (oper = "s") Then
txtoutput.Text = System.Math.Sqrt(first)
Else
If (oper = "%") Then
txtoutput.Text = (first * second) / 100
End If
End If
End If
End If
End If
End If
End Sub
Private Sub btnSubstract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubstract.Click
first = txtoutput.Text
txtoutput.Clear()
oper = "-"
End Sub
Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
first = txtoutput.Text
txtoutput.Clear()
oper = "*"
End Sub
Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
first = txtoutput.Text
txtoutput.Clear()
oper = "/"
End Sub
Private Sub btnSqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSqrt.Click
first = txtoutput.Text
txtoutput.Clear()
oper = "s"
End Sub
Private Sub btnPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercent.Click
first = txtoutput.Text
oper = "%"
End Sub
End Class
推荐答案
首先我想建议使用选择案例
First of all I would like to recommend usingselect case
select case oper
case '+'
txtoutput.Text = first + second
case '-'
txtoutput.Text = first - second
...
end select
这使代码更短,更易读。你也可以考虑失去神奇的按钮。如果你只是简单地创建一个函数会更容易测试:
this makes the code shorter and also better readable. Also you might consider to lose the magic pushbutton. It would be more easy to test if you would simply create a function like:
public function Calculate(byval first as string, byval second as string, byval oper as string) as string
即使没有用户界面,只需在直接窗口中即可轻松测试此功能。
按钮的动作看起来也很像,你可以简单地创建一个更通用的实现,但这取决于你。
它无法正常工作的原因是因为您的代码编写不完整。您从文本框中获取字符串值,并在各种计算中将其用作数字。 Basic一直是一种非常宽容的语言,在大多数情况下仍然是另一种方式。您可以尝试将计算包装为 Convert.ToString(System.Math.Sqrt(first))
。还记得要执行平方根,你必须输入数字,点击s,然后点击等于工作。
希望这会让你顺利进行。如果你遇到任何问题,请告诉我。
祝你好运!
You could easily test this function, even without a user interface just simply in the direct window.
The actions of the buttons also do look a like and you could simply create a more generic implementation but this is up to you.
The reason why it isn't working is because your code is not completely properly written. You get a string value from the textbox and use it as number in various calculations. Basic has always been a very forgiving language and still looks the other way in most cases. You could try to wrap the calculation something like Convert.ToString(System.Math.Sqrt(first))
. Also remember that to execute the square root you must enter the number, click s and then click equal to work.
Hopefully this will get you on your way. If you encounter any more problems, just let me know.
Good luck!
首先......你可能想要研究一下case语句。它可以将您所有疯狂的嵌套if语句转换为更易于阅读的语句:
First of all....you might want to look into using a case statement. It can turn all of your crazy nested if statements into this much easier to read statement:
Select Case oper
Case "+"
txtoutput.Text = first + second
Case "-"
txtoutput.Text = first - second
Case "*"
txtoutput.Text = first * second
Case "/"
txtoutput.Text = first / second
Case "s"
txtoutput.Text = System.Math.Sqrt(first)
Case "%"
txtoutput.Text = (first * Second()) / 100
End Select
其次,我能看到的唯一问题是,如果你试图做平方根,你就不要想要在按下相等按钮时填充第二个变量,因为它可能是空的。
如果您需要更多帮助,请提供更多信息,了解它是怎样的工作,你得到什么错误信息和/或你正在尝试什么情况给出了错误的结果。
Second, the only problem I can see is that if you are trying to do the square root you don't want to fill the second variable when the equal button is pressed because it will probably be empty.
If you need more help, please provide more information about how it isn't working, what error messages you get and/or what scenario you are trying that is giving an incorrect result.
错误的逻辑实现
For Sqrt&%
for Ex:1000-10%= 900
显示= 1作为答案请尽快帮助
Wrong Implementation of Logic
For Sqrt & %
for Ex: 1000-10%=900
it shows =1 as answer Please Help ASAP
这篇关于如何让我的sqrt按钮和百分比按钮在我的计算器上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!