Proposition => Write an application to calculate the diameter,circumference and area of a circle with a radius input by user.Assign the radius to a float variable.Declare a named constant PI with the value 3.14159. The application should output the diameter,circumference and area,each on a separate line
Translated into Thailand =>
เขียนโปรแกรมเพื่อคำนวณขนาดเส้นผ่าศูนย์กลาง,เส้นรอบวงและพื้นที่ของวงกลมที่มีรัศมีการป้อนข้อมูลโดยผู้ใช้ โดยรัศมีเป็นตัวแปรทศนิยม และประกาศ PI มีค่าเท่ากับ 3.14159 โปรแกรมควรจะแสดงผลเส้นผ่าศูนย์กลางมเส้นรอบวงและพื้นที่ให้อยู๋ในแต่ละบรรทัดที่แยกต่างหาก
Solution=>
float radius=4;
float PI=3.14159;
float d=radius*2;
float c=2*PI*radius;
float a=PI*radius*radius;
println("Diameter is "+d);
println("Circumference is "+c);
println("Area is "+a);
Out put =>
Diameter is 8
Circumference is 25.13272
Area is 50.26544
You can run code in http://processingjs.org/tools/processing-helper.html
อธิบายการทำงานของโปรแกรม =>
เป็นโปรแกรมที่ใช้หาเส้นผ่านศูนย์กลาง เส้นรอบวง และพื้นที่ของวงกลม โดยเราจะประกาศตัวแปลชื่อ radius มาเก็บค่าที่เราจะป้อนเข้าไปเมื่อกดรันค่า radius ก็จะถูกส่งไปคำนวนในสมการต่างๆ
และทำการปริ้นค่าออกมาเพื่อแสดงผลโดย1คำสั่งprintln จะได้1แถว ถ้าต้องการให้แยกแถวกันจิงต้องใช้คำสั่ง้ println 3ครั้ง
text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
ISBN 978-0-7637-3402-2
ไม่มีความคิดเห็น:
แสดงความคิดเห็น