如何使用双精度数组创建 for 循环?
How can I make a for-loop with array of doubles?
我正在尝试使用双精度数组创建一个 for 循环,但它不起作用。
代码如下:
double[] myDoubleArray = new double[10];
double piValue = 3.141592;
for (double i = 0; i < 10; piValue+) {
myDoubleArray[i] += i;
}
System.out.println(myDoubleArray[2]);
System.out.println(myDoubleArray[5]);
这是你想要做的吗?
for (int i=0; i < 10; ++i) {
myDoubleArray[i] = Math.PI + i;
}
这将填充您的双精度值数组,从 Pi 开始,十个元素中的每个元素递增 1。
即使你的数组是double数组,每个位置对应的索引仍然是int基元。
double[] doubleArray = {2.45, 4.45};
for(int i = 0; i < doubleArray.length; i++) {
System.out.println(doubleArray[i]); //Doing something with the double value
}
实际上数组索引使用 int。您可以按照以下代码
double[] myDoubleArray = new double[10];
double piValue = 3.141592;
for (int i = 0; i < 10; i++) {
myDoubleArray[i] = piValue + i;
}
System.out.println(myDoubleArray[2]);
System.out.println(myDoubleArray[5]);
我正在尝试使用双精度数组创建一个 for 循环,但它不起作用。
代码如下:
double[] myDoubleArray = new double[10];
double piValue = 3.141592;
for (double i = 0; i < 10; piValue+) {
myDoubleArray[i] += i;
}
System.out.println(myDoubleArray[2]);
System.out.println(myDoubleArray[5]);
这是你想要做的吗?
for (int i=0; i < 10; ++i) {
myDoubleArray[i] = Math.PI + i;
}
这将填充您的双精度值数组,从 Pi 开始,十个元素中的每个元素递增 1。
即使你的数组是double数组,每个位置对应的索引仍然是int基元。
double[] doubleArray = {2.45, 4.45};
for(int i = 0; i < doubleArray.length; i++) {
System.out.println(doubleArray[i]); //Doing something with the double value
}
实际上数组索引使用 int。您可以按照以下代码
double[] myDoubleArray = new double[10];
double piValue = 3.141592;
for (int i = 0; i < 10; i++) {
myDoubleArray[i] = piValue + i;
}
System.out.println(myDoubleArray[2]);
System.out.println(myDoubleArray[5]);