1

我对编程和测试一些我知道的东西非常陌生,我认为我会尝试制作一个程序。但无论输入如何,它都会以 0 响应。如果它很乱,我很抱歉,但在我的理解框架内,你能给出一个解决办法吗?

感谢您提供的任何帮助。

代码

class Program
{
    static void Main( string[] args ) {

        // Probability of Hitting

        Console.WriteLine( "Welcome to the Ranged Death Calculator, please enter the Ballistic Skill (BS) " + Environment.NewLine + "of the model firing." );
        float BallisticSkillf = Convert.ToSingle( Console.ReadLine() );
        float WSProbabilityf;
        if( BallisticSkillf <= 5 ) {
            WSProbabilityf = Convert.ToSingle( ( BallisticSkillf / 6 ) );
        }

        else {
            WSProbabilityf = Convert.ToSingle( ( ( 5 / 6 ) ) + ( 1 / 6 * ( ( BallisticSkillf - 6 ) / 6 ) ) );
        }

        // Probability of Wounding

        Console.WriteLine( "Please enter the toughness of your target" );
        float TargetToughnessf = Convert.ToSingle( Console.ReadLine() );

        Console.WriteLine( "Please enter the strength of the Weapon firing" );
        float WeaponStrengthf = Convert.ToSingle( Console.ReadLine() );

        float WoundProbabilityf = 0;

        if( WeaponStrengthf - TargetToughnessf >= 2 )
            WoundProbabilityf = ( (float)( 5 / 6 ) );

        else if( WeaponStrengthf - TargetToughnessf == 1 )
            WoundProbabilityf = ( (float)( 4 / 6 ) );

        else if( WeaponStrengthf - TargetToughnessf == 0 )
            WoundProbabilityf = ( (float)( 3 / 6 ) );

        else if( WeaponStrengthf - TargetToughnessf == -1 )
            WoundProbabilityf = ( (float)( 2 / 6 ) );

        else if( WeaponStrengthf - TargetToughnessf <= -2 )
            WoundProbabilityf = ( (float)( 1 / 6 ) );

        //Probability of failing saving throw

        Console.WriteLine( "Please enter the relevant armour save of the target, (So, 2, would be for 2+, , 3+ etc.) (Also, take into consideration the AP Value of the weapon fired)" );
        float nTargetRelevantSave = Convert.ToSingle( Console.ReadLine() );
        float TRProbabilityf = ( (float)( ( nTargetRelevantSave - 1 ) / 6 ) );

        // Number of shots fired

        Console.WriteLine( "Finally, please enter the number of shots fired with the weapon" );
        float nShotsFromWeapon = Convert.ToSingle( Console.ReadLine() );

        // All previous values to determine likely number of wounds inflicted
        float nWoundsInflictedf = ( (float)WSProbabilityf * (float)WoundProbabilityf * (float)TRProbabilityf * (float)nShotsFromWeapon );
        Console.WriteLine( "The number of wounds you are likely to inflict is " + (float)nWoundsInflictedf );
    }
}
4

3 回答 3

2

您的计算是整数,然后您将值转换为浮点数。您应该使用浮点数进行计算。

WoundProbabilityf = 5F / 6F;

除此之外,您正在对浮点数和 int 上的相等进行计算:

WeaponStrengthf - TargetToughnessf == 1

当您在右侧使用浮点数时,右侧的 int 变化会非常小。您可以更好地使用范围:

float fHitForce = WeaponStrengthf - TargetToughnessf;
fHitForce >= 1 && fHitForce < 2

浮动 MSDN

于 2012-10-13T23:28:30.287 回答
0

尝试

WoundProbabilityf = ( (float)( 5 / 6.0 ) );

如果分子和分母都是整数,则结果为整数。其中之一必须是浮动的。在变量的情况下,你可以做

WoundProbabilityf =  (float)( 5 / (n * 1.0) );
于 2012-10-13T23:31:49.593 回答
0
(float)(2 / 6)   

这将转换为浮动两个整数除法 2/6 = 0 的结果

工作尝试

 (float)2 / 6  
于 2012-10-13T23:28:10.330 回答