- Aug 22 Thu 2013 22:28
-
從Java開始
- Aug 26 Mon 2013 22:44
-
4# 陣列 (Array)
陣列
可用來儲存一組型態相同或相容的資料
定義陣列必須說明陣列名稱,元素的資料型態與元素的個數
資料型態 陣列名稱[] =new 資料型態[n]
n:陣列元素的個數
int xa[] = new int[100]; 定義一個名為xa的陣列, 可以存放100個整數
char a5[] = new char[15] ; 定義一個名為a5的陣列, 可以存放15個字元
double []a7 = new double [10] ; 定義一個a7陣列,可存放10個double浮點數
型態相容的資料 元素為double型態的陣列,可存放float, long, int ,short ,byte ,char 型態的資料
元素為int 型態的陣列,可存放 short, byte ,char 型態的資料,不能存放double, float ,long 型態的資料
陣列的元素與註標
陣列元素預設的初值
設定陣列元素的初值
double[] a1 = new double[3] ; // 定義含有三個元素的浮點數陣列
a1[0]= 10.0 ;
a1[1]= 90.1 ;
a1[2]= 9.9 ;
a1陣列的元素依序為10.0 , 90.1 , 9.9
我們也可以在定義陣列的敘述內,直接列舉元素的初值,用逗點隔開,如 :
int[] a2 = {100, 77 ,90, 88, 99} ;
定義有五個元素的整數陣列, 同時設定這些元素的初值 .
String[] name = {"Mary", "John", "Lisa", "Ellen" }
定義有四個元素的字串陣列,同時設定四個元素的初值.
n:陣列元素的個數
資料型態 [] 陣列名稱 =new 資料型態[n]
元素為int 型態的陣列,可存放 short, byte ,char 型態的資料,不能存放double, float ,long 型態的資料
陣列的元素與註標
| 陣列中可儲存資料的地方稱為元素,元素的表示法為 陣列名稱[註標] |
| 註標為元素的編號,假設陣列有n個元素,註標由0開始,第一個元素的註標為0,第二個為1,....最後一個元素為n-1 |
| 陣列的元素等同於一般的變數,不過它們皆有預設的初值(初值為0或近似0的值) |
| 所有陣列皆有length屬性,其值為陣列元素的個數,任何陣列之元素註標的下限為0,上限為元素個數減一 |
陣列元素預設的初值
| byte 0 | short 0 | int 0 | long 0L |
| float 0.0F | double 0.0D | char '\u0000' | boolean false |
| reference null |
設定陣列元素的初值
double[] a1 = new double[3] ; // 定義含有三個元素的浮點數陣列
a1[0]= 10.0 ;
a1[1]= 90.1 ;
a1[2]= 9.9 ;
a1陣列的元素依序為10.0 , 90.1 , 9.9
我們也可以在定義陣列的敘述內,直接列舉元素的初值,用逗點隔開,如 :
int[] a2 = {100, 77 ,90, 88, 99} ;
定義有五個元素的整數陣列, 同時設定這些元素的初值 .
String[] name = {"Mary", "John", "Lisa", "Ellen" }
定義有四個元素的字串陣列,同時設定四個元素的初值.
int []ar=new int[5]; ar[1]=59;ar[1]=59;ar[1]=59;ar[1]=59; |
| double [] br = {3.14,75.9 ,18.9}; |
| String[] name={ "kitty", "Snoopy" ,"garfield" }; |
- Aug 26 Mon 2013 22:42
-
4# 存取陣列內的所有元素
利用for敘述
Java5.0提供新版的for敘述:
for(型態 n : 陣列 ) {
迴圈本體
}
取值比大小,求總合;
int[] xa = {33,19 ,25 ,50, -8} ; for(int i=0 ;i<xa.length ;i++) { System.out.println( xa[i] ) ; } |
Java5.0提供新版的for敘述:
for(型態 n : 陣列 ) {
迴圈本體
}
| 此for敘述會由陣列中,一次取出一個元素,放入變數n內,然後將迴圈本體內的敘述執行一次, 然後重複此步驟,直到每個元素都處理一次才結束. int[] xa={33, 19, 25, 50, -8}; for(int n : xa) { Sysout.out.println(n); } |
取值比大小,求總合;
int []arr={100,77,90,88,99,39,91}; int max =arr[0]; int min =arr[0]; int sum = 0; for (int n =0; n<arr.length;n++){ sum+=arr[n]; if (arr[n]>max){ max =arr[n]; } if (arr[n]<min){ min=arr[n]; } } System. out .println("max=" +max+"\t"+ "min=" +min+"\t" +"sum=" +sum); |
- Aug 26 Mon 2013 22:42
-
4# 二維陣列
元素為一維陣列的陣列稱為二為陣列
int [] [] a = new int[4][] ; //有4橫列 ,編號0-3
a[0] = new int[6] ; 第0橫列有6個元素
a[1] = new int[3] ; 第1橫列有3個元素
[0][0] [0][1] [0][2] [0][3] [0][4] [0][5] [0][6]
[1][0] [1][1] [1][2]
每個陣列都可以不一樣長
int[] b[] = new int[4][5] ; 4橫列(Row)5直行(Column)
b.length 為4
直接指定二維陣列元素的初值:
int[ ][ ]aa = { {11, 55, 66} , {10, 66, 33} , {11, 22, 33, 55}, } ;
int [] [] a = new int[4][] ; //有4橫列 ,編號0-3
a[0] = new int[6] ; 第0橫列有6個元素
a[1] = new int[3] ; 第1橫列有3個元素
[0][0] [0][1] [0][2] [0][3] [0][4] [0][5] [0][6]
[1][0] [1][1] [1][2]
每個陣列都可以不一樣長
int[] b[] = new int[4][5] ; 4橫列(Row)5直行(Column)
b.length 為4
直接指定二維陣列元素的初值:
int[ ][ ]aa = { {11, 55, 66} , {10, 66, 33} , {11, 22, 33, 55}, } ;
- Aug 26 Mon 2013 22:40
-
3# switch 基本結構
case value1: 如果expression值為value1則由statements1開始執行
case value2:
statement1;
break;
case value3:
statement2;
break;
case value4:
case value5:
case value6:
case value6:
statement3;
break;
....
default:
statement;
當沒有任何case子句的value等於expression時,程式由default開始往下執行直到碰到break才結束
default子句可有可無,此子句不一定要放在switch的最後面
public class Page207 {
public static void main(String[] args) {
int days=0,month=0;
for(month=1;month<=12;month++){
switch(month){
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12:
days=31;
break;
case 4 :
case 6 :
case 9 :
case 11:
days=30;
break;
default:
days=28;
}
System. out.println(month+"月有" +days+"天");
}
}
}
1月有31天
2月有28天
3月有31天
4月有30天
5月有31天
6月有30天
7月有31天
8月有31天
9月有30天
10月有31天
11月有30天
12月有31天
- Aug 26 Mon 2013 22:37
-
3# 關於if,for ,while ,do-while ,switch
敘述(Statements)
| 敘述類型 | |
| if | 依條件成立與否,來決定執行的敘述 |
| for | 依條件成立與否,來決定重複執行的敘述 |
| while | 依條件成立與否,來決定重複執行的敘述 |
| do-while | 依條件成立與否,來決定重複執行的敘述 |
| switch | 依某運算式的值,來決定由某一組敘述開始往下執行 |
敘述一定要以分號做結束
敘述要在一對大括弧(稱為區塊)之內
| 大括弧可出現在另一對大括弧內=>構成巢狀區塊 |
{ 外層 所定義的變數可以在內層用 { 內層 所定義的變數不可與外層同名, 內層定義的變數不能拿去外層用 } } |
- Aug 26 Mon 2013 22:36
-
3# while ; do while 迴圈 基本結構
迴圈本體
}
迴圈本體中也要加入條件修正,避免無限迴圈
| do { 迴圈本體 } while ( condition ) |
| 執行迴圈,判斷條件(true),執行迴圈,.... |
| 要記得設初始化條件 迴圈本體中也要加入條件修正,避免無限迴圈 |
| 用法與for迴圈大同小異 |
public static void main(String[] args) { int x=1,sum=0; do { sum+=x; //1+2+...+10 x++; } while (x<=10); System. out .println("sum=" +sum); } | sum=55 |
- Aug 26 Mon 2013 22:33
-
3# if , if else ,if / else if 基本結構
條件判斷
| if (條件){ 敘述; } |
public static void main(String[] args) { int score =(int)(Math.random()*101); //以 int型態隨機產生0~100的值 System. out.println("分數=" +score); if (score>=60){ System. out.println("YA~" ); } } |
if( 條件 ){ 敘述; } else { 敘述; } |
public static void main(String[] args) { //亂數產生1~10的值 並印出是偶數或基數 int a = (int )(Math.random()*10+1); System. out.println(a); if(a%2==0){ System. out.println("偶數" ); } else{ System. out.println("奇數" ); } } |
if( 條件 ){ 敘述; } else if( 條件 ){ 敘述; } else if( 條件 ){ ... ... } else { 敘述; } |
public static void main(String[] args) { int a=(int )(Math.random()*11-5); System. out.println(a); if(a>0){ System. out.println("positive" ); } else if (a<0){ System. out.println("negative" ); } else { System. out.println("0" ); } } |
- Aug 26 Mon 2013 22:32
-
3# for迴圈 基本結構
對一組資料做相同的運算
for ( 初始化 ; 條件 ; 條件修正 ) {
迴圈本體
}
步驟 1. 執行[初始化]內的敘述一次
2. 判斷條件; TRUE 執行 , FALSE 停止for迴圈
3.執行迴圈本體
4.條件修正
5.回到步驟2開始
迴圈本體須由一對大括弧包起來,若省略大括弧 ,for(...)之後的第一個敘述視為迴圈本體
迴圈本體
}
2. 判斷條件; TRUE 執行 , FALSE 停止for迴圈
3.執行迴圈本體
4.條件修正
5.回到步驟2開始
public static void main(String[] args) {
// sum=1+2+....+8+9+10
int sum=0;
for(int i=1 ; i<=10;i++){
sum+=i;
}
System. out.println(sum);
}
- Aug 26 Mon 2013 22:29
-
3# Break, continue 搭配 標籤 ( Label )
- Aug 26 Mon 2013 22:22
-
3# break 以及 continue
| break | 停止整個迴圈的執行,然後執行迴圈之後的敘述. break需放在區塊內. |
| continue | 對於for迴圈:先執行遞增(條件修正)的部份,接著判斷條件,以便決定是否要繼續該迴圈 對於while , do while迴圈 :直接判斷條件,以便決定是否要繼續該迴圈 |
public static void main(String[] args) { int x= 0,sum=0; for(x=1;x<=10;x++){ if(x%5==0){ //判斷式中 break了for迴圈 break; } sum+=x; System. out.println("x=" +x+",sum="+sum); } System. out.println("Sum=" +sum); System. out.println("x=" +x); } | x=1,sum=1 x=2,sum=3 x=3,sum=6 x=4,sum=10 Sum=10 x=5 |
public static void main(String[] args) { int x= 0,sum=0; for(x=1;x<=10;x++){ if(x%5==0){ //在for迴圈中,遇到 continue 會先做完條件修正的動作,在判斷條件是否要繼續執行迴圈 continue; } sum+=x; System. out.println("x=" +x+",sum="+sum); } System. out.println("Sum=" +sum); System. out.println("x=" +x); } } | x=1,sum=1 x=2,sum=3 x=3,sum=6 x=4,sum=10 x=6,sum=16 x=7,sum=23 x=8,sum=31 x=9,sum=40 Sum=40 x=11 |
int y =0,sum1=0; y=1; while (y<=10){ if (y%5==0){ //當y除以5的於數是0的時候,所以當y值為5,10(5的倍數)的時候執行if敘述 y++; //y++ ....此行條件修正若沒加,在y值為5時會造成無限迴圈 continue ; //跳到迴圈起點做條件判斷 } sum1+=y; y++; //此行條正修正若沒加,會造成無限迴圈 } System. out .println("SUM=" +sum1); | SUM=40 |
- Aug 26 Mon 2013 22:20
-
2#輸入字串,然後由螢幕顯示這三個字串的長度
System.out.println( "輸入三個字串,然後由螢幕顯示這三個字串的長度" );
Scanner sca1 = new Scanner(System. in);
String s3 = sca1.next();
String s4 = sca1.next();
String s5 = sca1.next();
System. out.println("第1個字串長度:" +s3.length()+" 第2個字串的長度:" +s4.length()+" 第3個字串的長度:" +s5.length());