如何向数组中的每个元素添加特定的数字?JAVA
我试图为数组的每个元素添加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
* ...
*/
# 1 楼答案
试试这个:
用Gendarme替代的解决方案是用
a1[i]=(10*i)-10;
替换a1[i]=i-10;