![](/img/trans.png)
[英]Have to make a multiplication table with nested while loops. \t spacing messed up in triple digits
[英]Take two nested for loops. How can I make a variable count further up, after jumping cycling the loops?
我敢肯定答案很簡單,但我不明白。 這里以我的示例為例:
int matrix [][] = new int [rows][columns];
for (int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[0].length; j++)
{
mata[i][j] = Integer.parseInt (args[j]);
}
}
從內部循環到外部循環再回到內部循環后,如何使j
進一步向上計數? 通常,它將再次從零開始,這是不希望的,因為我需要下一個命令行參數。 我嘗試了幾件事,但是無法正常工作。
您可以添加一個外部變量:
int k = 0;
然后將最里面的行替換為:
mata[i][j] = Integer.parseInt(args[k]);
k += 1;
或計算一下:
mata[i][j] = Integer.parseInt(args[i * matrix.length + j]);
只需使用一個單獨的變量(下面的argc
):
int matrix[][] = new int[rows][columns];
for (int i = 0, argc = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++, argc++) {
mata[i][j] = Integer.parseInt(args[argc]);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.