วันอังคารที่ 6 สิงหาคม พ.ศ. 2556

Variable : Easy

Question... assign an initial value to a variable:
                  กำหนดตัวแปรเริ่มต้นคือ
         
                  int totalSeconds = 2549;

                  and convert this to hours, minutes and seconds. Do an example with pen and paper before you write the program.
                  และให้แปลงเป็นชั่วโมง,นาที,และวินาที โดยทำตัวอย่างกับกระดาษก่อนที่จะเขียนโปรแกรม

Answer...
                int totalSeconds = 2549;//ตัวแปรที่โจทย์กำหนดมาให้
                int hours=0;//กำหนดตัวแปรเพื่อหาชั่วโมง
                int minutes=0;//กำหนดตัวแปรเพื่อหาค่านาที
                int seconds=0;//กำหนดตัวแปรเพื่อหาค่าวินาที

                hours = totalSeconds/3600;//คำนวณเพื่อแปลงค่าหาชั่วโมง
                minutes = totalSeconds/60;//คำนวณเพื่อแปลงค่าหานาที
                seconds = totalSeconds%60;//คำนวณเพื่อแปลงค่าหาวินาที

                println("hours is "+hours);//แสดงค่าของชั่งโมง
                println("minutes is "+minutes);//แสดงค่าของนาที
                println("seconds is "+seconds);//แสดงค่าของวินาที

               แสดงผล  =  hours is 0
                                     minutes is 42
                                     seconds is 29

source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 45
ISBN : 0-13010922-3

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

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