Java 程式設計 -- 迴圈
Java 程式簡介運算式分枝迴圈陣列函數遞迴錯誤處理物件導向封裝繼承多型技巧函式庫字串數學正規表達式容器檔案網路資料庫視窗ThreadListener錯誤陷阱相關檔案相關資源教學錄影Eclipse考題解答訊息相關網站參考文獻最新修改簡體版English |
範例:從 1 印到 9 (使用 for)class For1 { public static void main(String[] args) { for (int i=1; i<=9; i++) System.out.println("第"+i+ "行"); } } 範例:從 1 印到 9 (使用 while)class While1 { public static void main(String[] args) { int i=1; while (i<=9) { System.out.println("第"+i+ "行"); i++; } } } 範例:九九乘法表public class Loop99 { public static void main(String[] args) { for (int i=2; i<=9; i++) for (int j=1; j<=9; j++) System.out.println(i+"*"+j+"="+i*j); } } 範例public class Hello { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hello World!"); int x=3, y=5; int z = x+y, w=x*y; System.out.println("x="+x+" y="+y+" "+" z="+z+" w="+w); float pi = 3.14f; double e = 2.71828; System.out.println("pi="+pi+" e="+e+" pi/e="+(pi/e)); // char *s = "Hello!"; String s = "Hello!"; char a='a'; System.out.println(s+a+pi); int sum = 0, i; for (i=1; i<=1000; i++) { if ((i%2==1) || (i%3==0)) sum += i; } System.out.println("i="+i+" sum="+sum); } } |
page revision: 3, last edited: 22 Sep 2011 03:03
Post preview:
Close preview