Java 程式設計 -- 迴圈

Java 程式

簡介

運算式

分枝

迴圈

陣列

函數

遞迴

錯誤處理

物件導向

封裝

繼承

多型

技巧

函式庫

字串

數學

正規表達式

容器

檔案

網路

資料庫

視窗

Thread

Listener

錯誤陷阱

相關檔案

相關資源

教學錄影

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);
 
    }
 
}

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License