简体   繁体   English

如何在循环中保存 k 的每个值?

[英]How do I save every value of k in the loop?

I am trying to create a code that calculates this infinite formula as many times as I want... 3 + 4/(2 3 4) - 4/(4 5 6) + 4/(6 7 8) - 4/(8 9 10) + 4/(10 11 12) - 4/(12 13 14)....我正在尝试创建一个代码,可以根据需要多次计算这个无限公式... 3 + 4/(2 3 4) - 4/(4 5 6) + 4/(6 7 8) - 4/( 8 9 10) + 4/(10 11 12) - 4/(12 13 14)....

static void Main(string[] args)
{
    double a = 2;
    double b = 3;
    double c = 4;

    double g = 2;
    double t = 3;
    double n = 4;
    int A = 0;

    while (A < 10)
    {
        double d = 4 / (a * b * c);
        double e = -4 / (g * t * n);
        double k = d + e;

        if (A % 2 == 0)
        {

            g += 2;
            t += 2;
            n += 2;
            A++;
        }
        else if (A % 2 == 1)
        {
            a += 2;
            b += 2;
            c += 2;
            A++;
            Console.WriteLine(k);
            
            //3.1415926
        }
    }
}

To save your values of k to a file, you can use a StreamWriter instead of Console.要将 k 的值保存到文件中,可以使用 StreamWriter 而不是 Console。

static void Main(string[] args)
{
    using StreamWriter writer = File.CreateText("output.txt");

    // ...

    writer.WriteLine(k);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为运行(每(毫秒)毫秒的)游戏创建一个“循环”? - How do I make a “loop” for a game, that runs every (number) of milliseconds? 如何保存Accord.Net K-Means的聚类结果以供重用? - How do I save the clustering results of Accord.Net K-Means for reuse? 如何提取每个输出的最后一个值? - how do i extract the last value of every output? 如何让 for 循环仅在玩家每次按下 Z 时循环 - How do I get the for loop to only loop every time the player presses Z 如何将更改的datagrid值保存到数据表中? - How do i save altered datagrid value into a datatable? 如何将值为 Id 的复选框列表保存到数据库中? - How do I save List of Checkboxes with value as Id to a database? ListPicker遇到了麻烦-如何保存回访的默认值? - ListPicker woes - how do I save the default value for return visits? 如何更新/保存步进值以备后用? - How Do I Update/Save Stepper Value for Later Use? 如何从循环中保存 datagridview 行中的单元格值并将其传递给每行的另一个循环? - How can I save cell value in datagridview rows from a loop and pass it to another loop for each row? 每次迭代或循环遍历文档列表时,如何创建新的 PDF 文件? - How do I create new PDF file every time i iterate or loop through the documents list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM