用sin,cos& amp;的方法问题棕褐色 [英] probleam with method sin, cos & tan

查看:108
本文介绍了用sin,cos& amp;的方法问题棕褐色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai,我的代码有问题,目前,我正在创建具有sin,cos& cos函数的科学计算器.棕褐色.我创建的代码无法正常工作,因为每次转换函数时,它将显示0作为答案.请在这里为我提供纠正方法的建议.

Hai, i have probleam with my code, currently i am create scientific calculator which have function sin, cos & tan. The code i create it not working because every time i convert the function it will show 0 as answer.Please advice me how to correct my code here.

Public Class Form1
    Dim total1, total2 As Double
    Dim [Operator] As String
    Const conversionFactor As Double = Math.PI / 180
Dim result As Double
        total2 = Val(answer.Text)

        Select Case [Operator]
             Case "SIN"
                result = Math.Sin(total1 / conversionFactor)
                answer.Text = result
            Case "COS"
                result = Math.Cos(total1 / conversionFactor)
                answer.Text = result
            Case "Tan"
                result = Math.Tan(total1 / conversionFactor)
        End Select
        answer.Text = result
    End Sub 

推荐答案

如果这是所有代码,则第一个问题似乎是您使用了total1 变量,但没有为其分配值.另外,您无需设置运算符,因此不会遇到任何情况.

在代码的开头放置一个断点,然后逐步查看中间结果和行为.
If this is all of the code, the first problem seems to be that you use total1 variable, but you don''t assign a value to it. Also you don''t set the operator so none of the cases are hit.

Put a breakpoint to the beginning of the code and go step-by-step to see intermediate results and behaviour.


处理此问题的常用方法是在
处设置一个断点
The usual way to handle this is to set a breakpoint at
total2 = Val(answer.Text)



这样一来,您就可以检查该方法执行过程中发生了什么.

这可能很有趣:在Visual Studio中调试 [



That will allow you to inspect what''s going on as the method executes.

This might be interesting: Debugging in Visual Studio[^]

Best regards
Espen Harlinn


具有立即常量"SIN","COS"等的情况是您所能发现的最糟糕的情况.如果这些是按钮,则为每个变量赋予一个语义名称,并分别向每个按钮添加一个处理程序,但共享处理程序代码,如果参数化.参数可以是某种枚举类型,而不是字符串:

Case with immediate constants "SIN", "COS" etc. is the worst you could invent. If these are buttons, give each variable a semantic name and add a handler to each button individually, but share the handler code, parametrize if. A parameter could be of some enumeration type, not string:

enum Operator { sin, cos, tag, /*...*/}



制作一个字典,根据每个运算符的值查找委托实例:



Make a dictionary which finds a delegate instance by each operator''s value:

using OperatorDictionary = System.Collections.Generic.Dictionary<Operator, OperatorApplication>;

//...

delegate double OperatorApplication(double left, double right);



如果您使用表达式解析文本,则同样的想法也适用.将其解析为某种逻辑结构,其中操作员代码是上面显示的枚举,而不是字符串.这样的事情.使用您的字符串类型的立即常量,甚至使用显式的字符串常量,您很快就会陷入支持问题.不要锁住自己.

—SA



The same idea works if you parse text with expression. Parse it into some logical structure, where the operator code is enumeration shown above, not string. Something like that. With your immediate constants of the string type, even with explicit string constants, you will get sunk in support issues pretty soon. Don''t lock yourself.

—SA


这篇关于用sin,cos&amp; amp;的方法问题棕褐色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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