使用陣列陣列加法、乘法
=================程式內容=================
public class array_add
{
public static void main(String[] args)
{
int a[][] = {{5,3,7},{7,3,11}};//宣告陣列a的數值
int b[][] = {{1,6,2},{1,3,3}};//宣告陣列b的數值
int c[][] = new int [2][3];//宣告陣列c存放運算結果
for (int i=0; i < a.length ;i++)
{
for (int j=0; j <a[i].length ;j++)
{
c[i][j] = a[i][j] + b[i][j];//陣列c = 陣列a +陣列b
System.out.print(c[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
}
=============執行後結果範例=============
6 9 9
8 6 14
Finished executing
=================程式內容=================
public class array_mul
{
public static void main(String[] args)
{
int a[][] = {{5,3,7},{7,3,11}};//宣告陣列a的數值
int b[][] = {{1,6,2},{1,3,3}};//宣告陣列b的數值
int c[][] = new int [2][3];//宣告陣列c存放運算結果
for (int i=0; i < a.length ;i++)
{
for (int j=0; j <a[i].length ;j++)
{
c[i][j] = a[i][j] * b[i][j];//陣列c = 陣列a *陣列b (唯一改變的地方)
System.out.print(c[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
}
=============執行後結果範例=============
5 18 14
7 9 33
Finished executing
=================程式內容=================
public class array_add
{
public static void main(String[] args)
{
int a[][] = {{5,3,7},{7,3,11}};//宣告陣列a的數值
int b[][] = {{1,6,2},{1,3,3}};//宣告陣列b的數值
int c[][] = new int [2][3];//宣告陣列c存放運算結果
for (int i=0; i < a.length ;i++)
{
for (int j=0; j <a[i].length ;j++)
{
c[i][j] = a[i][j] + b[i][j];//陣列c = 陣列a +陣列b
System.out.print(c[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
}
=============執行後結果範例=============
6 9 9
8 6 14
Finished executing
=================程式內容=================
public class array_mul
{
public static void main(String[] args)
{
int a[][] = {{5,3,7},{7,3,11}};//宣告陣列a的數值
int b[][] = {{1,6,2},{1,3,3}};//宣告陣列b的數值
int c[][] = new int [2][3];//宣告陣列c存放運算結果
for (int i=0; i < a.length ;i++)
{
for (int j=0; j <a[i].length ;j++)
{
c[i][j] = a[i][j] * b[i][j];//陣列c = 陣列a *陣列b (唯一改變的地方)
System.out.print(c[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
}
=============執行後結果範例=============
5 18 14
7 9 33
Finished executing
沒有留言:
張貼留言