บทที่ 7 > 7.5 การโปรแกรมแบบเชิงวัตถุ > 7.5.2 ภาษาโปรแกรมเชิงวัตถุ 7/36  
     
              การกระทำ main เป็นการกระทำหลักในการกระทำการ (run) โปรแกรม มีลักษณะเหมือนกับโปรแกรมหลักในการเขียนโปรแกรมภาษาปาสคาล โดยในการสร้างโปรแกรมภาษาจาวาจะใช้เครื่องหมาย "{" แทนข้อความ "begin" และเครื่องหมาย "}" แทนข้อความ end.  
     
Example : Pascal
   
PROGRAM   Average_5;
Var    sum , N, data : integer;
      average : real;  
BEGIN
      Write('The program will calculate the average of ');
      Writeln(' five integers you enter through the keyboard …');
      Writeln;
      sum := 0;
      N := 1;  
WHILE N <= 5 DO

      Begin

          Write('Please enter the number : ');
          Readln(data);
          sum := sum + data;
          N := N+1;
      End;

 
          Average := sum / 5;
          Write('The average of five numbers is ' , Average:6:2);
END.

 

 

Example : Java
   

import ConsoleInput.KeyboardInput;
Class   average_number
{
      public static String   s = "Please enter the number : ";

      public static void main(String args[])
      {
          int sum=0;
          int N = 1;
          System.out.print("The program will calculate the average of ");
          System.out.println ("five integers you enter through the keyboard ...");

          While (N <= 5)
          {
          system.out.print(s);
          int data = Integer.parseInt(KeyboardInput.read(Text));
          sum = sum + data;
          N++;
      }

      System.out.println ("The average of five numbers is " + sum/N);
}

 

 

 
     
 
ก่อนหน้า   ถัดไป