โค้ดภาษาจาวา คำนวณหาพื้นที่สี่เหลี่ยมผืนผ้า

» โค้ดโปรแกรม » Java » โค้ดภาษาจาวา คำนวณหาพื้นที่สี่เหลี่ยมผืนผ้า
  • 27 กุมภาพันธ์ 2017
  • 2 มีนาคม 2017
  • Java
  • Thai Open Code


ตัวอย่างโค้ดจาวา ในการคำนวณหาพื้นที่สี่เหลี่ยมผืนผ้า พร้อม flowchart และคำอธิบายโค้ดเพื่อให้เข้าใจการทำงานของโปรแกรมคำนวณหาพื้นที่สี่เหลี่ยมผืนผ้า ได้ดียิ่งขึ้น

สูตรการหาพื้นที่สี่เหลี่ยมผืนผ้า

พื้นที่สี่เหลี่ยมผืนผ้า = กว้าง x ยาว

 

ตัวอย่าง Flowchart

Flowchart คำนวณหาพื้นที่สี่เหลี่ยมผืนผ้า

ตัวอย่างโค้ด


//----------------------------------------------------------
// Author    : THAI OPEN CODE
// Author URI: https://www.thaiopencode.com
// Facebook  : https://www.facebook.com/ThaiOpenCode
//----------------------------------------------------------

import java.util.Scanner;

public class RectangleArea {

	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);

		System.out.print(" Input value of Width : ");
	    float Width = scan.nextFloat();
	 
	    System.out.print(" Input value of Long : ");
	    float Long = scan.nextFloat();
	      
	    System.out.printf(" Area of a Rectangle is %.2f ", Width * Long);
	    
		scan.close();
	}
}

 

อธิบายโค้ด

Width คือ ตัวแปรชนิด float เก็บค่า ความกว้าง
Long คือ ตัวแปรชนิด float เก็บค่า ความยาว
Width * Long คือ คำนวณหาพื้นที่สี่เหลี่ยมผืนผ้าตามสูตร (กว้าง x ยาว)

 

แสดงผล

โค้ดภาษาจาวา คำนวณหาพื้นที่สี่เหลี่ยมผืนผ้า