java问题望指教

来源:百度知道 编辑:UC知道 时间:2024/06/15 03:32:23
要求输出如下:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16



除了用4个for循环
德其他方法
比如要写10排

public class Out{
public static void main(String[] args){
for(int row = 1; row<=10; row++){
for(int col = 1; col<=4; col++){
System.out.print(row * col + " ");
}
System.out.println();
}
}
}

为什么要用四个for
两个就够了啊

1楼答案比较好。。 只不过把10和4改成你要的排数和行数 我刚刚咋就没想到呢。。

//n是你要几排
int[][] nums = new int[n][n];
for (int i = 1; i <= n; i++)
{
nums[0][n] = n;
nums[n][0] = n;
for (int j = 1; j < n; j++)
nums[n][j] = nums[n][j - 1] + n;
}

//再把nums print出来就好了 两个for loop搞定