5. ข้อความ

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.

 
รหัสที่ 5-9  โปรแกรมคำนวณเลขโดดตรวจสอบตามมาตรฐาน EAN-13 (แบบใช้วงวน)
import java.util.Scanner;
// โปรแกรมคำนวณเลขโดดตรวจสอบตามมาตรฐาน EAN-13
public class EAN13 {
  public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    System.out.print("ตัวเลข 12 หลัก = ");
    String d = kb.nextLine();
    int s = 0;
    for (int k=0; k<=11; k++) {
      int v = Integer.parseInt(d.substring(k, k+1));
      if (k % 2 == 0) {
        s += v;   // ตำแหน่งคู่ 
      } else {
        s += 3*v; // ตำแหน่งคี่ 
      }
    }
    System.out.println("Check Digit คือ " + (10-(s%10))%10);
  }
}
©2009 S.Prasitjutrakul