我创建了一个数组xr_arr
,用于存储变量xr
和xr
存储100个值。
如何在不编辑xr_arr
步骤的情况下从x+=
输出前24个值?
static void xn()
{
double r = 3.9;
for (double x = 0; x <= 1; x+= 0.01)
{
double xr = r * x * (1 - x);
double [] xr_arr= new double[]{xr};
for (int y = 0; y <23; y++) {
// Console.WriteLine(xr_arr[y]);
}
}
答案 0 :(得分:3)
您的代码需要修改如下
将xr_arr
移到第一个for
在构建它的for
之后移动显示数组的for
。
新代码
double r = 3.9;
double[] xr_arr = new double[100];
// build xr_arr
for (double x = 0; x <= 1; x += 0.01)
{
double xr = r * x * (1 - x);
int index = (int) (x * 100);
xr_arr[index] = xr;
}
var length = 23;
// display xr_arr
for (int y = 0; y < length; y++)
{
Console.WriteLine(xr_arr[y]);
}
我建议您构建两个用于构建的方法和一个用于显示数组的方法。您将仅赢得,代码将更易于维护和可重复使用。
static void xn()
{
string data = " abc df fd";
var xr_arr = Build(100);
Display(xr_arr, 23);
}
public static double[] Build(int size)
{
double r = 3.9;
double[] xr_arr = new double[size];
double step = 1.0 / size;
for (int index = 0; index < size; index++)
{
var x = index * step;
double xr = r * x * (1 - x);
xr_arr[index] = xr;
}
return xr_arr;
}
public static void Display(double[] xr_arr, int size)
{
var length = Math.Min(xr_arr.Count(), size);
for (int y = 0; y < length; y++)
{
Console.WriteLine(xr_arr[y]);
}
}
答案 1 :(得分:2)
static void xn()
{
double r = 3.9;
int count = 100;
double[] xr_arr = new double[count];
for (int x = 0; x < count; x += 1)
{
var incrementValue = (double)x / (double)100;
double xr = r * incrementValue * (1 - incrementValue);
xr_arr[x] = xr;
}
for (int y = 0; y < 23; y++)
{
Console.WriteLine(xr_arr[y]);
}
}
已完成更改,
答案 2 :(得分:2)
你可能想要
var xr_arr = new double[100];
预先分配数组(如果您知道它将包含正好100个元素)或者,如果您不知道预先确切的项目数,则可以使用List<double>
将新号码添加到。所以这样做:
static void xn()
{
double r = 3.9;
var n = 0;
// I think the number of elements could (theoretically) be different from 100
// and you could get an IndexOutOufRangeException if it is 101.
var increment = 0.01d; // 1.0d/300.0d; // <== try this.
var n_expected = 100; // 300 // <= with this.
var x_arr = new double[n_expected];
// alternative: if you cannot be absolutely certain about the number of elements.
//var x_list = new List<double>();
for (double x = 0; x <= 1; x += increment)
{
double xr = r * x * (1 - x);
x_arr[n++] = xr;
// x_list.Add(xr); // alternative.
}
for (int y = 0; y <23; y++)
{
Console.WriteLine(xr_arr[y]);
// Console.WriteLine(xr_list[y]); // alternative.
}
}
浮点精度问题
请注意,与一般预期不同,浮点值仍然存储在有限数量的位中,因此受编码分辨率和舍入效果的影响,如果在循环中执行加法等操作,则会使精度降低。期望实数在计算机上的工作方式与纯数学相同,这是一个常见的错误。请查看what every computer scientist should know about floating point arithmetic及其引用的文章。
在这个例子中,如果你增加了1.0 / 300,期望获得300个元素,你就会遇到问题。
答案 3 :(得分:2)
您需要使用List&lt;&gt;对象,以便您可以添加项目。请参阅以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
xn();
}
static void xn()
{
double r = 3.9;
List<double> xr_arr = new List<double>();
for (double x = 0; x <= 1; x += 0.01)
{
double xr = r * x * (1 - x);
xr_arr.Add(xr);
}
Console.WriteLine(string.Join(",", xr_arr.Take(24).Select(a => a.ToString()).ToArray()));
}
}
}
答案 4 :(得分:1)
在每个for
柜台;正在创建new
数组。在for
循环之前声明它,然后继续!
static void xn()
{
double r = 3.9;
double [] xr_arr= new double[100];
for (double x = 0; x <= 1; x+= 0.01)
{
double xr = r * x * (1 - x);
xr_arr[x]= xr;
for (int y = 0; y <23; y++) {
Console.WriteLine(xr_arr[y]);
}
}
}