วันจันทร์ที่ 22 กรกฎาคม พ.ศ. 2556

condition


Question....Given the float variables x1,x2,y1,y2,and m ,write a code segment to find the slope of a line through the two points (x1,y1)and(x2,y2). Use the formula

ให้ตัวแปรลอยๆ x1, x2, y1, y2, และ m ให้เขียนโค้ดหาความชันของเส้นผ่านจุดสองจุด (x1, y1) และ (x2, y2) 


ใช้สูตร


              m = y2-y1

                     x2-x1

to determine the slope of the line. If x1 equals x2,the line is vertical and the slope is undefined. The segment should display the slope with an appropriate label.If the slope is undefined, it should display the message “Slope undefined.”


กำหนดความชันของเส้น ถ้า x1 เท่ากับ x2 เส้นแนวตั้งและความชันจะหาค่าไม่ได้ ส่วนการแสดงผลความชันที่เหมาะสมควรเป็นป้ายประกาศ  ถ้าความชันหาค่าไม่ได้ก็ควรแสดงข้อความ "ความชันหาค่าไม่ได้"


Write a code :

 //ประกาศและกำหนดตัวแปรเพื่อให้โค้ดสามารถแสดงผลให้เราดูได้
int y1= 10;
int y2= 15;
int x1= 15;
int x2= 15;//หากกำหนดค่าของ x2 มากกว่า x1 จะทำให้เราสามารถหาค่าของความชั่นได้
int m=0;
if(x2-x1>0){//เงื่อนไขบอกว่าถ้า x2-x1 แล้วมากกกว่า 0 จะสามารถหาค่าความชันได้โดย statement ด้านล่าง
    m=y2-y1/(x2-x1);
    println(+m);
}else{//ถ้าไม่เป็นไปตามเงื่อนไขด้านบนจะทำตาม statement ด้านล่างนี้
    System.out.print("Slope undefined.");
}




source : Programming and problem solving with Java
Nell Dale and Chip Weems. -- 2nd ed. page 271
ISBN-13 : 978-0-7637-3402-2

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

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