array

16
Array 1 มมมม

Upload: nitigan-nakjuatong

Post on 28-Jun-2015

574 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Array

ArrayArray1 มิ�ติ�1 มิ�ติ�

Page 2: Array

Array 1 มิ�ติ�

51

51 63 97 24

Page 3: Array

Array 1 มิ�ติ�

51

51 63 97 24

int score;

Page 4: Array

Array 1 มิ�ติ�

51

51 63 97 24

int score;

int score[] = new int[4];

Page 5: Array

Array 1 มิ�ติ�

51 63 97 24

int score[] = new int[4];

[0] [1] [2] [3]

score[0] = 51score[1] = ??score[2] = ??score[3] = ??

Page 6: Array

Array 1 มิ�ติ�: การประกาศติวแปร•int s[] = new int[4];•String s[] = new String[4];•float s[] = new float[4];

int s[]; s = new int[4];

Page 7: Array

Array 1 มิ�ติ�: การให้�ค่�าติวแปร•int s[] = new int[4];•s[0] = 10; s[2] = 30;•s[1] = 20; s[3] = 40;

•int s[] = {10, 20, 30, 40}

10 20 30 40[0] [1] [2] [3]

Page 8: Array

Array 1 มิ�ติ�• ให้�นักศ�กษาสร�าง class ขึ้��นัมิาให้มิ�• ให้�ชื่��อว�า TestArray1•เพื่��อใชื่�เก�บค่ะแนันัขึ้องนักศ�กษา

• และให้�ทำ#าการรบค่�า int จำ#านัวนั 1 ค่�า เพื่��อนั#าไปก#าห้นัดขึ้นัาดขึ้อง

จำ#านัวนั นัศ.

Page 9: Array

Array 1 มิ�ติ�public static void main(String[]

args){... รบค่�า int 1 ค่�า (num)...int score[] = new int[num];

}

ถ้�าติ�องการร( �ขึ้นัาดขึ้องArray ให้�ใชื่� score.length

Page 10: Array

Array 1 มิ�ติ�public static void main(String[]

args){... รบค่�า int 1 ค่�า (num)...int score[] = new int[num];... ให้�รบค่ะแนันัขึ้อง นัศ. ติามิจำ#านัวนั num โดยใชื่� For Loop ...

} Enter num of Student: 4 Enter score[0]: 10 Enter score[1]: 20 Enter score[3]: 30 Enter score[4]: 40

Page 11: Array

Array 1 มิ�ติ�public static void main(String[]

args){... รบค่�า int 1 ค่�า (num)...int score[] = new int[num];

for(int s=0;s<score.length;s++){

...รบค่�าใส�ในัแติ�ละชื่�อง...}

}Enter num of Student: 4 Enter score[0]: 10 Enter score[1]: 20 Enter score[3]: 30 Enter score[4]: 40

Page 12: Array

Array 1 มิ�ติ�public static void main(String[]

args){... รบค่�า int 1 ค่�า (num)...int score[] = new int[num];

for(int s=0;s<score.length;s++){

System.out.print(“Enter score[”+ s + “]: ”);score[s] = scan.nextInt();

}}

Enter num of Student: 4 Enter score[0]: 10 Enter score[1]: 20 Enter score[3]: 30 Enter score[4]: 40

Page 13: Array

Array 1 มิ�ติ�public static void main(String[]

args){... รบค่�า int 1 ค่�า (num)...int score[] = new int[num];for(int s=0;s<score.length;s++){

System.out.print(“Enter score[”+ s + “]:”);score[s] = scan.nextInt();

}... แสดงค่�าทำ+�ร บออกมิา...

}Enter num of Student: 4 Enter score[0]: 10 Enter score[1]: 20 Enter score[2]: 30 Enter score[3]: 40 Score are: 10 20 30 40

Page 14: Array

Array 1 มิ�ติ�String output = “ ”;

for(int p=0;p<score.length;p++){output = output + score[p] + “ ”;

}

System.out.println(“Score are”+output); Enter num of Student: 4

Enter score[0]: 10 Enter score[1]: 20 Enter score[2]: 30 Enter score[3]: 40 Score are: 10 20 30 40

Page 15: Array

Array 1 มิ�ติ�ให้�เปล+�ยนั การรบค่�าค่ะแนันัจำาก Keyboard

เป,นัการ Random ค่ะแนันัระห้ว�าง 1-100 โดยสร�างเป,นั Method ขึ้��นัมิาให้�ชื่��อว�า rnd

และเร+ยกใชื่�ผ่�านั Method นั+�•--------------------------------------------public static int rnd(int st, int ed){

... return ...;

} score[s] = rnd(1,

100);

Page 16: Array

Array 1 มิ�ติ�

String output = “ ”;

for(int p=0;p<score.length;p++){output = output + score[p] + “ ”;

}System.out.println(“Score are”+output);

ให้�เปล+�ยนัเป,นัMethod แบบRecursive