C# 为什么变量不被新值覆盖

C# 为什么变量不被新值覆盖,c#,C#,我正在尝试制作一个简单的控制台应用程序,它执行数学函数 我的问题是,在MakeFirstStageCalculations的循环过程中,剩余部分没有被新的剩余部分覆盖,我缺少什么 static void Main(string[] args) { Console.WriteLine("Generator A starting value: ");//put 65 string generatorAValue = Console.ReadLine(); Console.Wr

我正在尝试制作一个简单的控制台应用程序,它执行数学函数

我的问题是,在
MakeFirstStageCalculations
循环过程中,剩余部分没有被新的剩余部分覆盖,我缺少什么

static void Main(string[] args)
{
    Console.WriteLine("Generator A starting value: ");//put 65
    string generatorAValue = Console.ReadLine();
    Console.WriteLine(MakeFirstStageCalculations(Convert.ToDecimal(generatorAValue), 16807));
}

public static int MakeFirstStageCalculations(decimal value, decimal coefficient, decimal division = 2147483647)
{

    decimal multiplicationValue = value * coefficient;
    decimal remainder = MakeRemainder(multiplicationValue, division); //first remainder starting value if value is 65 i posted what values should be returned. 
    int donein = 0; // how long while loop needed to complete.
    #if DEBUG
    int goodvalue = 0;
    #endif
    while (remainder != 0)
    {
        remainder = MakeRemainder(remainder, division);//PROBLEM IS RIGHT HERE,it should return new remained and overwrite.
        Console.WriteLine(remainder);

        #if DEBUG
        if (goodvalue == 10)
        {
            break;
        }
        goodvalue++;
        #endif
        donein++;
    }
    /*First 4 remainder values should be:
     * 1092455
     * 1181022009
     * 245556042
     * 1744312007
     */
    return donein;
}

private static decimal MakeRemainder(decimal multiplicationValue, decimal division)
{
    decimal z = multiplicationValue / division;
            z = multiplicationValue - z;
            return z;
}
如果目前的解释不够充分,我愿意在评论中详细说明

正确的前4个数字是:

1092455

1181022009

245556042

1744312007

根据任务,公式为:

  • 用户输入值(65)与系数(16807)相乘

  • 乘以的结果除以(2147483647)

  • 结果是分裂的残余

  • 该结果将成为下一次迭代的初始编号


  • 我的公式错了吗?

    主要问题是,您没有将计算出的余数用作下一次迭代的新开始值。起始值保存在变量
    中,变量
    乘法值
    只计算一次。之后,您将计算每个迭代相同的余数。你所计算的是

    42 % 100 = 42
    
    这和

    ((((42 % 100) % 100) % 100) % 100) = 42
    
    因为您每次都可以将
    42%100
    替换为
    42

    您需要再次触发初始乘法,并计算新的余数。代码应该如下所示:

    int startValue = 65;
    int coefficient = 16807;
    int divisor = 2147483647;
    
    int iterationValue = startValue; // (2)
    for (int i=0; i<10; i++) {
        long multipliedValue = (long)iterationValue * (long)coefficient;
        int remainder = (int)(multipliedValue % divisor);
        Console.WriteLine(i+": "+remainder);
        iterationValue = remainder;  // (1)
    }   
    
    如您所见,您将在顶部找到前四个想要的值

    第二个问题是,在计算中/仅使用类型
    int
    。这可能会导致问题。在第一次迭代之后,您将要计算乘法值
    1092455*16807
    ,即
    18360891185
    。但是,此值不适合
    int32
    变量。您会得到一个所谓的“整数溢出”,这将导致一个负值。仅使用整数类型/值,您将获得以下输出:

    0: 1092455
    1: 1181022001
    2: -1902071305
    3: -670839007
    4: -502038649
    5: 1847162897
    6: 1243194391
    7: -647765503
    8: 747286439
    9: 1158806769
    

    通过在正确的位置将值/类型更改为
    long
    ,您可以在不获得负值的情况下进行乘法和余数计算。您可能也想考虑使用值来避免这个问题。

    我想它应该是除法%乘法。@速度258<代码> 65×16807=1092455 < /代码>。除以较大的数字(如2147483647)得到余数仍然是1092455。因此,除非你把除法变小,否则你将得到相同的余数。你想干什么?您希望通过输入
    65
    得到什么其他结果?使用输入显示计算的数学值
    65
    @speed258
    1092455%2147483647
    仍将是
    1092455
    。为什么你认为结果会是
    1181022009
    ?@speed258好吧,这不是你写的。您既没有在代码中说过也没有在代码中编写用不同的值替换
    value
    。在您的代码和说明中,它仍然是
    65
    (并且没有更改)。请重新编写您要计算的数学步骤和问题的详细说明。@speed258
    1092455%2147483647
    的结果是
    1092455
    。但是如何从
    1092455
    获得所需的值
    1181022009
    ?不清楚您要执行什么计算。请列出有关如何从
    result
    中的值到
    newresult
    中的值的数学步骤。
    0: 1092455
    1: 1181022001
    2: -1902071305
    3: -670839007
    4: -502038649
    5: 1847162897
    6: 1243194391
    7: -647765503
    8: 747286439
    9: 1158806769