为什么我的程序保持零响应?

时间:2012-10-13 23:25:08

标签: c# casting floating-point

我对编程非常陌生并且测试一些我知道我会尝试制作程序的东西。但无论输入如何,它都会以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 );
    }
}

3 个答案:

答案 0 :(得分:2)

您的计算是以整数计算,然后将值转换为浮点数。你应该用浮点数来计算。

WoundProbabilityf = 5F / 6F;

除此之外,您正在对浮点数进行计算,而对int进行计算:

WeaponStrengthf - TargetToughnessf == 1

当你在右边使用浮点数时,点击右边的int的变化将非常低沉。您可以更好地使用范围:

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

Float MSDN

答案 1 :(得分:0)

(float)(2 / 6)   

这将抛出两个整数除法2/6 = 0

的结果

开始尝试

 (float)2 / 6  

答案 2 :(得分:0)

尝试

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

如果分子和分母都是整数,则结果为整数。其中一个必须漂浮。如果是变量,你可以做

WoundProbabilityf =  (float)( 5 / (n * 1.0) );