8. Array 1 มิติ ให้นักศึกษาสร้าง class ขึ้นมาใหม่ ให้ชื่อว่า TestArray1 เพื่อใช้เก็บคะแนนของนักศึกษา และให้ทำการรับค่า int จำนวน 1 ค่า เพื่อนำไปกำหนดขนาดของจำนวน นศ .
9. Array 1 มิติ public static void main(String[] args){ ... รับค่า int 1 ค่า (num)... int score[] = new int[ num ]; } ถ้าต้องการรู้ขนาดของ Array ให้ใช้ score .length
10. 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
11. 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
12. 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
13. 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
14. 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
15. Array 1 มิติ ให้ เปลี่ยน การรับค่าคะแนนจาก Keyboard เป็นการ Random คะแนนระหว่าง 1-100 โดยสร้างเป็น Method ขึ้นมาให้ชื่อว่า rnd และเรียกใช้ผ่าน Method นี้ -------------------------------------------- public static int rnd (int st, int ed){ ... return ...; } score[s] = rnd (1, 100);