วันจันทร์ที่ 22 กรกฎาคม พ.ศ. 2556
Condition
Question...Give this code fragment:
if (height>= minHeight){
if(weight>=minWeight){
System.out.print("Eligible to serve.");
}else{
System.out.print("Too light to serve.");
}
}else{
if (weight>=minWeight){
System.out.print("Too short to serve.");
}else{
System.out.print("Too short and too light to serve.");
}
}
a. What is the output when height exceeds minHeight and weight exceeds minWeight?
สิ่งใดจะถูกส่งออกเมื่อ height มากกว่า minHeight และ weight มากกว่า minWeight
Answer...เราสามารถหาคำตอบได้โดยการประกาศและกำหนดค่าดังนี้
int height =12;//มีค่ามากกว่า minHeight เป็นไปตามที่โจทย์กำหนด
int minHeight =10;//มีค่าน้อยกว่า height
int weight =3;//มีค่ามากกว่า minWeight เป็นไปตามที่โจทย์กำหนด
int minWeight = 0;//มีค่าน้อยกว่า weight
เมื่อเราได้ประกาศและกำหนดค่าต่างๆเหล่านี้ไปแล้ว output ของ code นี้คือ Eligible to serve.
a. What is the output when height is less than minHeight and weight is less than minWeight?
สิ่งใดจะถูกส่งออกเมื่อheight น้อยกว่า minHeight และ weight น้อยกว่า minWeight
Answer...เราสามารถหาคำตอบได้โดยการประกาศและกำหนดค่าดังนี้
int height =12;//มีค่าน้อยกว่า minHeight เป็นไปตามที่โจทย์กำหนด
int minHeight =14;//มีค่ามากกว่า height
int weight =3;//มีค่าน้อยกว่า minWeight เป็นไปตามที่โจทย์กำหนด
int minWeight = 5;//มีค่ามากกว่า weight
เมื่อเราได้ประกาศและกำหนดค่าต่างๆเหล่านี้ไปแล้ว output ของ code นี้คือ Too short and too light to serve.
source : Programming and problem solving with Java
Nell Dale and Chip Weems. -- 2nd ed. page 267
ISBN-13 : 978-0-7637-3402-2
ป้ายกำกับ:
Assignment1
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
ไม่มีความคิดเห็น:
แสดงความคิดเห็น