我正在尝试为数组的每个元素添加10但我得到了:
[-10.0, -9.0, -8.0, -7.0, -6.0, -5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0]
我想要的是
[-10.0, 0.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 110.0]
任何帮助将不胜感激。 这是我的代码:
import java.util.Arrays;
public class ReportCardClient {
public static void main(String[] args) {
double[] a1 = new double[13];
for(int i=-0; i < a1.length; i++) {
a1[i]=i-10;
}
System.out.println(Arrays.toString(a1));
/*
* create an array marks for 13 double values
* assign values, using a loop, such that,
* first value is -10
* second value is 0
* third value is 10
* fourth value is 20
* ...
*/
答案 0 :(得分:1)
试试这个:
for(int i=0; i < a1.length; i++) {
a1[i]=(i-1)*10;
}
Gendarme的替代解决方案是将a1[i]=i-10;
替换为a1[i]=(10*i)-10;