Sample Program JAVA (5 ตัวอย่าง)

Sample Program JAVA   ( 5 ตัวอย่าง) พร้อมอธิบาย


อย่าลืมน่ะ...การเขียนโปรแกรมตัวหนังสือพิมพ์เล็กพิมพ์ใหญ่มันไม่เหมือนกัน  !!

Sample 1   //การกรอกข้อมูลใส่//



import java.io.*;       //  ต้องการใช้คลาส io หาค่าเฉลี่ย
class PeawXX         
 //  เวลา Save as ตั้งชื่อต้องเหมือนกัน น่ะ
{

public static void main (String[] args)throws IOException  //  ให้รับค่าเข้ามา
{
BufferedReader stdin=new BufferedReader (new InputStreamReader (System.in));      
 //  รับค่าเข้ามา
String b1,b2,b3,b4;            
 //  รับค่า   b1, b2, b3, b4   เป็นข้อความ
System.out.print("What's your name. =  ");
         //  แสดงข้อความ What's your name. 
b1= stdin.readLine();         
  //  รับค่าตัวหนังสือมาใส่ใน b1
System.out.print("Last name. =  ");         
//  แสดงข้อความ Last name. 
b2= stdin.readLine();                    
 //  รับค่าตัวหนังสือมาใส่ใน b2
System.out.print("You by a nickname. =  ");     
//  แสดงข้อความ You by a nickname.  
b3 = stdin.readLine();            
//  รับค่าตัวหนังสือมาใส่ใน b3
System.out.print("How old. =  ");  
           //  แสดงข้อความ How old. 
b4 = stdin.readLine();            
//  รับค่าตัวหนังสือมาใส่ใน b4
}
}

images by free.in.th
T

======================================================


Sample 2  //การทดสอบตัวเลข//

import java.io.*;       //  ต้องการใช้คลาส io หาค่าเฉลี่ย
class java_ex755                 
 //  เวลา Save as ตั้งชื่อต้องเหมือนกัน น่ะ
{
{
public static void main (String[] args)throws IOException 
//  ให้รับค่าเข้ามา
{
{
BufferedReader stdin=new BufferedReader (new InputStreamReader (System.in));         
 //  รับค่าเข้ามา
int money,amount;             
//  ประกาศตัวแปรเก็บค่าของ money,amount
         String c;         
//  รับค่า c เป็นข้อความ
         String b1;     
 //  รับค่า b1 เป็นข้อความ
    do{        
//  เริ่มต้น
    do{ 
       //  เริ่มต้น

System.out.println("\n---ADD MONEY ( >=100 )--- = ");      //  แสดงข้อความตามวงเล็บ
     b1= stdin.readLine();                 
 //  รับค่าตัวหนังสือมาใส่ใน  " Name"
     money =Integer.parseInt(b1);
    //   รับค่าตัวหนังสือใน " b1 " มาใส่ใน  " money "
          }while(money<=99);           
//  จบคำสั่งตามเงื่อนไข
System.out.println("Do you want to try again Y=(Yes) N=(No) = ");    
//  แสดงข้อความตามวงเล็บ
     c= stdin.readLine();              
      //  รับค่าตัวหนังสือมาใส่ใน  " c"
           }while(c == "y");                       
//  จบคำสั่งตามเงื่อนไข

System.out.println("***********Bye Bye**********\n");    //  แสดงข้อความตามวงเล็บ
 }
}

images by free.in.th

======================================================

Sample 3  //การทดสอบการเพิ่มค่า//


import java.io.*;       //  ต้องการใช้คลาส io หาค่าเฉลี่ย
class PeawX            
 //  เวลา Save as ตั้งชื่อต้องเหมือนกัน น่ะ
{
            public static void main(String[] args) throws IOException    
//  ให้รับค่าเข้ามา
            {
                int x,y;
         //  ประกาศตัวแปร x และ y เป็นจำนวนเต็ม
                x = 1; 
         //  ให้ x มีค่าเท่ากับ 1
                y = x++;     
 //  นำ x ส่งให้ y แล้วเพิ่มค่า x ขึ้น 1 ค่า
           do                   
 //  คำสั่งเริ่มการวนซ้ำ
                    {

System.out.println("X =  " +x+"  Y = "+y);       //  แสดงข้อความ 
          y = ++x;                    
 //  เพิ่ม x ขึ้น 1 ค่า แล้วส่งให้ตัวแปร y
System.out.println("X =  " +x+"  Y = "+y);     
 //  แสดงข้อความ 
                     } while (x!=10);         
 //  จบคำสั่งวนซ้ำเมื่อเข้าเงื่อนไข
            }
}

images by free.in.th



======================================================

Sample 4   //การลบเลขจำนวนเต็ม//

import java.io.*;       //  ต้องการใช้คลาส io หาค่าเฉลี่ย

class java_ex1           //  เวลา Save as ตั้งชื่อต้องเหมือนกัน น่ะ
{
            public static void main(String[] args) throws IOException     //  ให้รับค่าเข้ามา
            {
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));   //  รับค่าเข้ามา
         String innum1="";    //  รับค่า innum1 เป็นข้อความ
         String innum2="";    //  รับค่า innum2 เป็นข้อความ
          int num1;                    //  ประกาศตัวแปรเก็บค่าของ num1
          int num2;                    //  ประกาศตัวแปรเก็บค่าของ num2
         System.out.print("Figures number1:");   //  แสดงข้อความ (Figures number1)
             innum1=stdin.readLine();              //  รับค่าตัวหนังสือมาใส่ใน  "innum1"
         System.out.print("Figures number2:");    //  แสดงข้อความ (Figures number2)
             innum2=stdin.readLine();              //  รับค่าตัวหนังสือมาใส่ใน  "innum2"
         System.out.println("=========");          //  แสดงข้อความ (=========)
             num1=Integer.parseInt(innum1);  //  แสดงให้ค่า innum1  เท่ากับค่า num1
             num2=Integer.parseInt(innum2);  //  แสดงให้ค่า innum2  เท่ากับค่า num2
         System.out.println(num1 - num2);            //  แสดงค่า  num1 - num2
            }
}
images by free.in.th

======================================================

Sample 5  //การเรียงจำนวนตัวเลขมากไปน้อย//
import java.io.*;       //  ต้องการใช้คลาส io หาค่าเฉลี่ย 
class  PeawXXXXX           //  เวลา Save as ตั้งชื่อต้องเหมือนกัน น่ะ 
{
public static void main(String[] args) throws IOException     //  ให้รับค่าเข้ามา  
            {
     BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));   //  รับค่าเข้ามา  
            int n=0;           //  ประกาศตัวแปรเก็บค่าของ  n  
            int ini=0;        //  ประกาศตัวแปรเก็บค่าของ  ini   
            int min=0;     //  ประกาศตัวแปรเก็บค่าของ  min  
            int max=0;     //  ประกาศตัวแปรเก็บค่าของ  max  
                        String innum="";     //  รับค่า innum เป็นข้อความ  
            int num=0;      //  ประกาศตัวแปรเก็บค่าของ  num  
            int sum=0;      //  ประกาศตัวแปรเก็บค่าของ  sum  
            int num1=0;  //  ประกาศตัวแปรเก็บค่าของ  num1  
            int num2=0;  //  ประกาศตัวแปรเก็บค่าของ  num2  
            int num3=0;  //  ประกาศตัวแปรเก็บค่าของ  num3  
            int num4=0;  //  ประกาศตัวแปรเก็บค่าของ  num4  
            int num5=0;  //  ประกาศตัวแปรเก็บค่าของ  num5  
            int mm[]=new int[5];    //    เป็นตัวแปรอาร์เรย์ใช้กำหนดค่าตัวเลข  
            for (n=1;n<=5 ; n++)           // ค่าของตัวแปรควบคุมอาจถูกกำหนดแร้วแต่เรา  
                         {
            System.out.print("Input number [no."+n+"] : ");           //  แสดงข้อความ    
                innum = stdin.readLine();  //   รับค่าตัวหนังสือมาใส่ใน  "innum"   
                num = Integer.parseInt(innum);    //   รับค่าตัวหนังสือใน "innum " มาใส่ใน   "num"                      
                        mm[1]=mm[0];
                        if (n==1)                    //  เงื่อนไข  
                      {
                        mm[0]=num;
                        num1=mm[0];
            System.out.println("mm[0] : "+mm[4]); //  แสดงข้อความ   
                       }
                        if (n==2)                    //  เงื่อนไข   
                      {
                        mm[1]=num;
                        num2=mm[1];
            System.out.println("mm[1] : "+mm[3]); //  แสดงข้อความ   
                      }
                        if(n==3)                     //  เงื่อนไข   
                     {
                        mm[2]=num;
                        num3=mm[2];
            System.out.println("mm[2] : "+mm[2]); //  แสดงข้อความ  
                     }
                        if (n==4)                    //  เงื่อนไข  
                    {
                        mm[3]=num;
                        num4=mm[3];
            System.out.println("mm[3] : "+mm[2]); //  แสดงข้อความ   
                    }
                        if (n==5)                    //  เงื่อนไข  
                   {
                        mm[4]=num;
                        num5=mm[4];
            System.out.println("mm[4] : "+mm[1]); //  แสดงข้อความ  
                    } 
            }
            System.out.println("              num1 =  "+num5);   //  แสดงข้อความ  
            System.out.println("              num2 =  "+num4);   //  แสดงข้อความ  
            System.out.println("              num3 =  "+num3);   //  แสดงข้อความ  
            System.out.println("              num4 =  "+num2);   //  แสดงข้อความ  
            System.out.println("              num5 =  "+num1);   //  แสดงข้อความ  
            }
}
images by free.in.th
======================================================