[英]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.