วันพุธที่ 17 กรกฎาคม พ.ศ. 2556

Variable

= Easy =

Proposition =>  what is the type of the value 0 and 3.2 .(ค่าของ 0 และ 3.2 คือค่าชนิดใด)

Solution =>
int x = 0;
float y = 3.2;

Explain =>
 int คือชนิดของตัวเลขที่เป็นจำนวนเต็ม
float คือชนิดของตัวเลขที่เป็นทศนิยม หรือเลขยกกำลัง

From : Java concepts  => ISBN : 0-471-69704-4

= Medium =

Proposition => Sum of Two Numbers
write a program that stores the integers 62 and 99 in variables, and stores their sum in a variable named total. (เขียนโปรแกรมที่เก็บค่าตัวเลขจำนวนเต็ม 62 และ 99 ไว้ในตัวแปร นำค่าที่ได้จากการบวก มาใส่ไว้ในตัวแปรชื่อ total)

Solution =>
 //variable
int a = 62;
int b = 99;
int total = a+b;
println ("total="+total);

Explain => จากโจทย์ให้เขียนโปรแกรมที่เก็บค่าจำนวนเต็ม 62 และ 99 ไว้ในตัวแปร และเก็บผลบวกที่ได้ไว้ในตัวแปรชื่อว่า total


//variable จะเป็นการประกาศตัวแปร
int a = 62;  //เป็นการสร้างตัวแปร a ขึ้นมาเพื่อเก็บค่าจำนวนเต็ม 62 ตามโจทย์บอก
int b = 99; //เป็นการสร้างตัวแปร b ขึ้นมาเพื่อเก็บค่าจำนวนเต็ม 99 ตามโจทย์บอกint total = a+b; //เป็นการสร้างตัวแปร total ขึ้นมาเพื่อเก็บค่าที่ได้จาก a+b เป็นจำนวนเต็ม
println ("total="+total); //เป็นการใช้คำสั่งเพื่อแสดงค่าผลลัพธ์ที่ได้ของ total ออกมา  โดยที่ผลลัพธ์คือ 161


From : Starting out with Java => ISBN : 0-321-48110-0


= Hard =

Proposition => Define Variables that sum and count currently contain 60 and 80, respective. If sum represents the sum of a group of integer value and count represent the number of value, let's find the average value (กำหนดตัวแปรชื่อ sum และ count เป็นค่าคงที่ 60 และ 80 ตามลำดับ ถ้าผลรวมแสดงให้เห็นถึงผลรวมของกลุ่มของค่าจำนวนเต็มและนับเป็นตัวแทนของจำนวนมูลค่าให้หาค่าเฉลี่ย)

Solution =>
float sum = 60;
float count = 80;
float average = sum / count;
println ("average = "+average);

Explain =>
float sum = 60; //เป็นการปรัการและกำหนด ค่าที่โจทย์ให้มาของตัวแปร sum
float count = 80; //เป็นการปรัการและกำหนด ค่าที่โจทย์ให้มาของตัวแปร count
float average = sum / count; //เป็นการหาค่าเฉลี่ยนแทนด้วยตัวแปรชื่อ average โดยสูตรของการหาค่าเฉลี่ยคือ ผลรวมทั้งหมด(sum) หารด้วยจำนวนทั้งหมด (count)
println ("average = "+average); //เป็นการแสดงค่าของ average ที่หาได้จากสมการ

 From : Programming and Problem solving with Java => ISBN_13 : 978-0-7637-3402-2

 

ไม่มีความคิดเห็น:

แสดงความคิดเห็น