Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-循环结束结果_C# - Fatal编程技术网

C#-循环结束结果

C#-循环结束结果,c#,C#,我将尽我所能解释这一点,所以请耐心听我说。我在做一个游戏,基本上有100码。你从100开始,它在一个循环中,生成数字并从100码中减去它们。一旦数字达到0,循环将停止 请看下面的代码: int yardsLeft = 100; // this is on a loop until 'yardsLeft = 0' if (yardsLeft >= 80) { // 10% chance of generating a number 80-100 // 20% chan

我将尽我所能解释这一点,所以请耐心听我说。我在做一个游戏,基本上有100码。你从100开始,它在一个循环中,生成数字并从100码中减去它们。一旦数字达到0,循环将停止

请看下面的代码:

int yardsLeft = 100;
    // this is on a loop until 'yardsLeft = 0'
if (yardsLeft >= 80)
{
    // 10% chance of generating a number 80-100
    // 20% chance of generating a number 40-80
    // 70% chance of generating a number 1-40

    // Say it hits the 10% chance and generates the number 85 - there's 15 yards left
    // it will pass through the if statements entering the `if (yardsLeft < 40)` - say that hits 20 once more. at the end once the yardsLeft finally equals 0, the yards added up each loop will be over 100. in this case 120
    -------------------------------
    // but if the generated number generates a 70% chance and hits a number 1-20, it's going to stay in the `yardsLeft > 80` if statment-
    // therefore having the potential to exceed the number '100' once the `yardsLeft = 0`
}
else if (yardsLeft >= 40 && yardsLeft <= 79) { } // this would activate if the 20% chance got generated
if (yardsLeft < 40)
    {
    // 10% chance of generating a number 30-39
    // 20% chance of generating a number 10-29
    // 70% chance of generating a number 1-9
    }
这是我的变量

public static Random r = new Random();
public static int gained;
public static int yardsLeft = 100;
public static int i = 0;
public static int chance = r.Next(1, 101);

不要只是从最上面的if/else if/else语句返回数字,而是等到块的末尾再返回。这将允许您收集所有逻辑,以便在一个位置进行计算

private int Play(int yardsLeft) // Pass in the total yards left here
{
    var yards = 0;

    if (yardsLeft >= 80)
    {
        yards = // The result of your 10/20/70% calculation
    }
    else if (yardsLeft >= 40) { } // Same deal
    else { } // Again, same as in the if

    return yards > yardsLeft ? yardsLeft : yards;
}

你也可以修改它,以提供一个字符串来表示是否得分。(将
yardsLeft
参数设置为
ref
,以便您可以在该方法中调整剩余的码数。)

我认为您没有理解。每经过一个循环,就会产生一个数字。这些数字加起来直到它达到100,也使得雅兹列夫等于0。我这样做的方法会使最终结果超过100,因为当它循环,并且到达'yardsLeft>=80'时,这不是正确的计算,所以它会加上100以上。我需要精确到100。但我不知道这和你的有什么不同?为什么不在那里澄清问题?为什么最终结果不能超过100?如果你到达99码,下一个随机数是100,只需返回
(100-99)=1
。你没有领会我的意思-生成的最终随机数是多少并不重要,因为你将超过100。因此,您只需返回上一次跑步和100之间行驶的距离。
private int Play(int yardsLeft) // Pass in the total yards left here
{
    var yards = 0;

    if (yardsLeft >= 80)
    {
        yards = // The result of your 10/20/70% calculation
    }
    else if (yardsLeft >= 40) { } // Same deal
    else { } // Again, same as in the if

    return yards > yardsLeft ? yardsLeft : yards;
}