为什么变量没有变化,但是应该变化 [英] Why doesn't a variable change but should

查看:113
本文介绍了为什么变量没有变化,但是应该变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法确定为什么变量不更改但应该更改.请参见底部的代码块.

变量是Radius.由于每次单击执行"按钮时,变量光度"和冲浪温度"都会更改,因此半径"也应更改.但是,除非文本框"tbxStarRadSol"中的工作半径"也被更改,否则它不会更改.

如果我没记错的话,由于变量SurfTemp因代码顶部的随机性(SpectralLevel和TempRating)而改变,然后此变量改变了光度,那么两者都应改变Radius的值.这不会发生.

当然,由于半径不会更改,因此音量也不会更改.但是,如上所述,这两个变量的值都应更改,因为它们是从确实发生更改的其他变量派生而来的.

有人可以帮忙吗?

I am having trouble determining why a variable does not change but it should change. See code block at bottom.

The variable is Radius. Since the variables Luminosity and SurfTemp change each time the Do It button is clicked, then Radius should also change. However it does not change unless the Working Radius in the textbox "tbxStarRadSol" is also changed.

If I am not mistaken, since the variable SurfTemp is changed due to the randoms at the top of code (SpectralLevel and TempRating), and then this variable changes Luminosity, then both should also change the value for Radius. This does not happen.

Of course, since Radius does not change, Volume does not change. However, as said, both these variables should change in value since they are derived from other variables that do change.

Can anyone help?

Private Sub btnDoIt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDoIt.Click
    Const StefanBoltzmann As Double = 0.000000056704 : Const PI As Double = 3.1415926536
    Dim SpecLvl As New Random : Dim TRate As New Random
    Dim TempDiff, Luminosity, InnerBioRad, OuterBioRad, SurfTemp, Volume, MeanDens As Double
    Dim ApproxLife, AbsMag, RadMeters, Radius, Mass, SpectralLevel, TempRating, TempMin As Double
    Dim TempLvl As Double
    Dim SpectralClass As String

    SpectralClass = CStr(tbxSpectralClass.Text)
    RadMeters = CDbl(tbxStarRadSol.Text) * 695500000

    SpectralLevel = SpecLvl.Next(10)
    TempRating = TRate.Next(100)

    Select Case SpectralClass
        Case "O"
            TempDiff = 30000
            TempMin = 30000
        Case "B"
            TempDiff = 20000
            TempMin = 10000
        Case "A"
            TempDiff = 2500
            TempMin = 7500
        Case "F"
            TempDiff = 1500
            TempMin = 6000
        Case "G"
            TempDiff = 1000
            TempMin = 5000
        Case "K"
            TempDiff = 1500
            TempMin = 3500
        Case "M"
            TempDiff = 1500
            TempMin = 2000
        Case "o"
            TempDiff = 30000
            TempMin = 30000
        Case "b"
            TempDiff = 20000
            TempMin = 10000
        Case "a"
            TempDiff = 2500
            TempMin = 7500
        Case "f"
            TempDiff = 1500
            TempMin = 6000
        Case "g"
            TempDiff = 1000
            TempMin = 5000
        Case "k"
            TempDiff = 1500
            TempMin = 3500
        Case "m"
            TempDiff = 1500
            TempMin = 2000
        Case Else
            MsgBox("Invalid Spectral Class", 0)
            tbxSpectralClass.Text = ""
            tbxSpectralClass.Focus()
    End Select

    TempLvl = (SpectralLevel * 100) + TempRating
    SurfTemp = (((1000 - TempLvl) / 1000) * TempDiff) + TempMin

    Luminosity = StefanBoltzmann * (SurfTemp ^ 4) * 4 * PI * (RadMeters ^ 2)
    AbsMag = 4.83 - (2.5 * (Log((Luminosity / 3.839E+26), 10)))
    Mass = ((Luminosity / 3.839E+26) ^ (1 / 3.5)) * 1.98892E+30
    InnerBioRad = (0.7 * (Sqrt(Luminosity / 3.839E+26))) * 149597870691
    OuterBioRad = (3 * (Sqrt(Luminosity / 3.839E+26))) * 149597870691
    ApproxLife = ((Mass / 1.98892E+30) ^ -2.5) * 10000000000
    Radius = (Sqrt((Luminosity / 3.839E+26) / ((SurfTemp / 5778) ^ 4))) * 695500000
    Volume = (4 * PI * (Radius ^ 3)) / 3
    MeanDens = Mass / Volume

    tbxStarTemp.Text = Format(SurfTemp, "##,#0.0000")
    tbxStarRadius.Text = Format(Radius, "##,#0.0000")
    tbxStarLumin.Text = Format(Luminosity, "0.00000000E+00")
    tbxStarAbsMag.Text = Format(AbsMag, "##,#0.0000")
    tbxStarMass.Text = Format(Mass, "0.00000000E+00")
    tbxStarVolume.Text = Format(Volume, "0.00000000E+00")
    tbxStarMeanDens.Text = Format(MeanDens, "##,#0.0000")
    tbxStarIBRad.Text = Format(InnerBioRad, "##,#0.0000")
    tbxStarOBRad.Text = Format(OuterBioRad, "##,#0.0000")
    tbxStarLife.Text = Format(ApproxLife, "##,#0.0000")
End Sub



感谢您的帮助.

rmfr

================================================== ==================

引用DavidAuld
作为旁注,我看到您在宣布PI.没必要.它内置于.Net中,只是包括System.Math",您将看到它可供您使用:)

实际上,PI是一个数学领域.在基于Web的帮助中查找它.您仍然需要通过在表格开头下方使用以下内容来声明它:

公共建筑监理处为Double

================================================== ==================

@ Kschuler

应该说这是程序的一部分,该程序可以计算恒星初等(即恒星)和恒星系统行星的许多参数.

实际上,我确实尝试过使用最少的数据类型来避免消耗太多内存.但是,由于大量恒星基数(甚至行星)的数字可以计算为1e-39到1e75(对于Hypergiants有时更大),从很小到很大的数字,我回过头来,只是更改了所有数据类型加倍,以确保在计算期间我不会遇到任何问题.

您至少可以通过调试和观察来进行试验,以查看计算结果是否如您期望的那样精确和精确."

已经有.计算通常可以精确到15到21个有效数字(通过使用实际显示最多24个数字的计算器进行检查).对我来说,即使对于我编写的Astrogation程序,这也足够准确.

如果这对您没有帮助,您能否在代码中发布更多有关文本框使用什么值的信息,..."

只有两个文本框可以接受用户的输入(即进行所有计算所需的全部):
tbxSpectralClass,它接受单个字母(O,B,A,F,G,K,M,o,b,a,f,g,k,m)和
tbxStarRadSol,它基本上接受一个十进制数,该十进制数等于以Sol为单位的星形半径(1 Sol单位半径= 695,500,000米).

所有其他文本框用于显示计算值.

"以及这些提供的值:
SpectralLevel = SpecLvl.Next(10)
TempRating = TRate.Next(100)"

好吧,这需要一些解释.恒星原色按光谱类型分类,该光谱类型由光谱类别,光谱级别和光度类别组成.例如,我们的星星是G2V. G =光谱等级(O,B,A,F,G,K,M),2 =光谱等级(范围0至9),V =光度等级(0,Ia,Ib,II,III,IV,V ,VI,VII).

频谱级别= SpecLvl.Next(10)为频谱级别生成一个数字0到9.现在,我想起来了,我应该添加一个文本框为用户列出该值.与TempRating相同.做起来很简单.

TempRating = TRate.Next(100)生成一个0到99的数字,并将其添加到Spectral Level以获得0到999的TempLvl.我生成此值是为了计算Stellar Primary的表面温度(SurfTemp).由于恒星的温度范围最低为1000开氏温度,最高为30,000,因此我想为每个温度范围生成一个等于0.0到0.999的数字.例如,G级恒星的范围为开尔文(Kelvin)5000至6000度.此过程使我可以生成以1度为增量(有时是十分之一度)的度数.

不需要亮度等级,因为工作半径是通过查阅Hertzsprung-Russell图(一个巨大的图像文件:2195pxw x 3047pxh x 8bpp @ 428,137字节)得出的,我怀疑它是否可以嵌入帖子中.

然后在两个步骤的过程中使用两个随机数:
TempLvl =(SpectralLevel * 100)+ TempRating
SurfTemp =((((1000-TempLvl)/1000)* TempDiff)+ TempMin
计算星体的表面温度.

尽管光度"变量使用RadMeters变量,但由于SurfTemp变量会发生变化(每次单击执行"按钮时),因此光度"会发生变化.而且由于Radius变量使用Luminosity和SurfTemp来计算恒星的半径,因此Radius也应该更改,但不会更改.

只是尝试一下,我将所有Doubles更改为Decimal.但是,不断出现Overload Exception错误.作为程序员,我还不够成熟,不知道如何处理此类错误.或者,我只是还没有阅读过有关如何处理重载异常"错误的信息.



Thanks for any help.

rmfr

====================================================================

quote by DavidAuld
Hi, as a side note, i see you declaring PI. No need. It is built into .Net, just ''include System.Math'' and you will see it available for you :)

Actually, PI is a Math Field. Looked it up in the web-based Help. You still need to declare it by using the following just under the start of the form:

Public Const PI As Double

====================================================================

@ Kschuler

Should have said this is part of a program that calculates many of the parameters for a Stellar Primary (i.e. star) and the planets of the stellar system.

Actually I did try using bare minimum data types to keep from consuming so much memory. However, since quite a few numbers for Stellar Primaries (and even planets) can calculate to a range from 1e-39 to 1e75 (sometimes larger for the Hypergiants), very small to very large numbers, I went back and just changed all data types to Doubles so as to make sure I would stop running into problems during the calculations.

"You could at least experiment by debugging and watching to see if the calculations are as accurate and precise as you are expecting them to be."

Already have. Calculations are usually accurate up to 15 to 21 significant digits (checked by using a calculator that actually displays up to 24 digits). For me, that is accurate enough, even for the Astrogation program I wrote.

" If that doesn''t help you, could you post more information in your code about what values you are using for the textboxes,..."

There are only two textboxes that accept input from the user (that is all that is needed to make all the calculations):
tbxSpectralClass which accepts a single letter (O, B, A, F, G, K, M, o, b, a, f, g, k, m), and
tbxStarRadSol which accepts, basically, a decimal number equivalent to a star''s radius in Sol Units (1 Sol Unit Radius = 695,500,000 meters).

All other textboxes are for displaying the calculated values.

" and what values these provide:
SpectralLevel = SpecLvl.Next(10)
TempRating = TRate.Next(100)"

Well, this needs some explanation. Stellar Primaries are catalogued by Spectral Type which is comprised of Spectral Class, Spectral Level, and Luminosity Class. For example, our star is a G2V. G = Spectral Class (O, B, A, F, G, K, M), 2 = Spectral Level (ranges 0 to 9), and V = Luminosity Class (0, Ia, Ib, II, III, IV, V, VI, VII).

Spectral Level = SpecLvl.Next(10) generates a number 0 to 9 for the Spectral Level. Now that I think of it, I should add a textbox to list this value for the user. Same for TempRating. Simple enough to do.

TempRating = TRate.Next(100) generates a number 0 to 99 and is added to the Spectral Level to get a TempLvl of 0 to 999. I generate this so as to calculate the Stellar Primary''s Surface Temperature (SurfTemp). Since the temperature ranges for star''s are a minimum of 1000 degrees Kelvin up to 30,000, I wanted to generate a number equal to 0.0 to 0.999 for each of these ranges. For example, a G class star ranges from 5000 to 6000 degrees Kelvin. This procedure allows me to generate degrees going by 1 degree increment (sometimes tenth of a degree).

Luminosity Class is not needed since the Working Radius is derived from consulting the Hertzsprung - Russell Diagram (a huge image file: 2195pxw x 3047pxh x 8bpp @ 428,137 bytes) which I doubt could be embedded within the post.

Then the two randoms are used in a two step process:
TempLvl = (SpectralLevel * 100) + TempRating
SurfTemp = (((1000 - TempLvl) / 1000) * TempDiff) + TempMin
to calculate the star''s surface temperature.

Although the Luminosity variable uses the RadMeters variable, since the SurfTemp variable changes (with each click of the Do It button), Luminosity changes. And since Luminosity and SurfTemp are used by the Radius variable to calculate the star''s radius, Radius should also change, but it does not.

Just to try it, I changed all the Doubles to Decimal. However, kept getting Overload Exception errors. And I am not sophisticated enough as a programmer to know what to about this kind of error. Or, I just have not read up on how to handle Overload Exception errors.

推荐答案

在阅读代码时,我注意到的第一件事是您正在使用Double Data.类型.我在将数据类型与截断问题一起使用时遇到了问题 [ ^ ].切换到十进制数据类型后,我的代码运行良好. 这是文章 [
如果那对您没有帮助,是否可以在代码中发布更多有关文本框使用哪些值以及它们提供哪些值的信息:
The first thing I noticed when reading your code is that you are using the Double Data Type. I had issues using that data type with a truncate issue[^]. After switching to the Decimal data type my code worked just fine. Here is the article[^] that helped explain what was going on in my code. But I''m not sure this is really your issue...or that changing to Decimal would work. You seem to need to get into really really small numbers and depending upon the values you use in your calculations I''m not sure a Decimal would hold the values you need. You could at least experiment by debugging and watching to see if the calculations are as accurate and precise as you are expecting them to be.

If that doesn''t help you, could you post more information in your code about what values you are using for the textboxes, and what values these provide:
SpectralLevel = SpecLvl.Next(10)
TempRating = TRate.Next(100)


这篇关于为什么变量没有变化,但是应该变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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