7. แถวลำดับ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 7-1  โปรแกรมแสดงสถิติจำนวนนักเรียนที่ได้คะแนนต่างๆ
import java.util.Scanner;
import java.io.*;
// โปรแกรมแสดงสถิติจำนวนนักเรียนที่ได้คะแนนต่างๆ  
public class Stat {
  public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(new File("c:/java101/java101.txt"));
    int[] p = new int[11];   
    while (in.hasNext()) {
      in.nextDouble();      // อ่านรหัสประจำตัวทิ้งไป 
      int v = in.nextInt(); // อ่านคะแนน (เป็นจำนวนเต็ม)
      if (0 <= v && v <= 10) p[v]++;
    }
    for (int v = 0; v < 11; v++) {
      System.out.println(p[v] + " คนได้ " + v + " คะแนน");
    }
  }
}
©2009 S.Prasitjutrakul