Java 程式設計 -- 函數
Java 程式簡介運算式分枝迴圈陣列函數遞迴錯誤處理物件導向封裝繼承多型技巧函式庫字串數學正規表達式容器檔案網路資料庫視窗ThreadListener錯誤陷阱相關檔案相關資源教學錄影Eclipse考題解答訊息相關網站參考文獻最新修改簡體版English |
範例一:計算平方的函數class Func1 { public static void main(String[] args) { int x = square(5); System.out.println("x="+x); } public static int square(int n) { return n*n; } } 範例二:陣列相加class Func1 { public static void main(String[] args) { int a[][] = {{1,2}, {3,4}}; int b[][] = {{5,6}, {7,8}}; int c[][] = new int[2][2]; add(a, b, c); System.out.println("=======a======="); print(a); System.out.println("=======b======="); print(b); System.out.println("=======c======="); print(c); } public static void add(int x[][], int y[][], int z[][]) { for (int i=0; i<z.length; i++) for (int j=0; j<z[i].length; j++) { z[i][j] = x[i][j] + y[i][j]; } } public static void print(int x[][]) { for (int i=0; i<x.length; i++) { for (int j=0; j<x[i].length; j++) System.out.print(x[i][j]+" "); System.out.println(); } } } |
page revision: 1, last edited: 04 Nov 2010 02:52
Post preview:
Close preview