array
TRANSCRIPT
![Page 1: Array](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/1.jpg)
ArrayArray1 มิ�ติ�1 มิ�ติ�
![Page 2: Array](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/2.jpg)
Array 1 มิ�ติ�
51
51 63 97 24
![Page 3: Array](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/3.jpg)
Array 1 มิ�ติ�
51
51 63 97 24
int score;
![Page 4: Array](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/4.jpg)
Array 1 มิ�ติ�
51
51 63 97 24
int score;
int score[] = new int[4];
![Page 5: Array](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/8.jpg)
Array 1 มิ�ติ�• ให้�นักศ�กษาสร�าง class ขึ้��นัมิาให้มิ�• ให้�ชื่��อว�า TestArray1•เพื่��อใชื่�เก�บค่ะแนันัขึ้องนักศ�กษา
• และให้�ทำ#าการรบค่�า int จำ#านัวนั 1 ค่�า เพื่��อนั#าไปก#าห้นัดขึ้นัาดขึ้อง
จำ#านัวนั นัศ.
![Page 9: Array](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/9.jpg)
Array 1 มิ�ติ�public static void main(String[]
args){... รบค่�า int 1 ค่�า (num)...int score[] = new int[num];
}
ถ้�าติ�องการร( �ขึ้นัาดขึ้องArray ให้�ใชื่� score.length
![Page 10: Array](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.pub/reader033/viewer/2022060119/558fcfa21a28ab451b8b45f0/html5/thumbnails/16.jpg)
Array 1 มิ�ติ�
String output = “ ”;
for(int p=0;p<score.length;p++){output = output + score[p] + “ ”;
}System.out.println(“Score are”+output);
ให้�เปล+�ยนัเป,นัMethod แบบRecursive