java array基础编程
Writeaprogramtocreatea5*52Darrayofintegersandpopulateit(thatmeansfillinthevalues)acco...
Write a program to create a 5*5 2D array of integers and populate it(that means fill in the values) according to the following rule: the (i,j)-th element should have the value|i-j|. So (1,1)-th element should be 0, as should the (2,2)-th, etc; and the (2,4)-th element should be 2.
Print out the array values to show your answers 展开
Print out the array values to show your answers 展开
1个回答
展开全部
貌似翻译过来是:写一个二维的数组,五行五列,然后这个数组符合的规则是(i,j)位置的值为i-j的绝对值。可以这样写:
int a[][]=new int[5][5];
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
a[i][j]=Math.abs(i-j);
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
System.out.print(a[i][j]+" ")
}
System.out.println();
}
int a[][]=new int[5][5];
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
a[i][j]=Math.abs(i-j);
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
System.out.print(a[i][j]+" ")
}
System.out.println();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询