|
 |
|
ขอพี่ๆช่วยแนะนำวิธีแก้โจทย์หน่อยได้มัยคับ เกี่ยวกับการใช้ switch case คับ |
|
 |
|
|
 |
 |
|
จงเขียนโปรแกรม PHP โดยใช้ SWITCH-CASE เท่านั้น รับค่าตัวเลขเข้ามา 10 ตัว (10-99) แล้วหาว่ามีจำนวนเลขคู่กี่จำนวน อะไรบ้าง และเลขคี่กี่จำนวน อะไรบ้าง แล้วรับค่า A, B, C, D มาคำนวณตามสมาการ A, B, C, D ดังนี้
สมาการ ของ A = นำตัวเลขคู่มาบวกกัน, นำตัวเลขคี่มาลบกัน
สมาการ ของ B = นำตัวเลขคู่มาคูณกัน, นำตัวเลขคี่มาบวกกัน
สมาการ ของ C = นำตัวเลขคู่ยกกำลังสองแล้วบวกกัน, นำตัวเลขคี่มา Divide ด้วย 5 แล้วบวกกัน
สมาการ ของ D = นำตัวเลขคู่ยกกำลังสองแล้วบวกกัน, นำตัวเลขคี่มา Mod ด้วย 5 แล้วบวกกัน
? ให้แสดงผลลัพธ์ดังนี้
ตัวเลขที่ป้อนเข้ามา: 23, 45, 32, 10, 86, 56, 98, 82, 66, 79
เลขคี่: 23, 45, 79 (3 จำนวน)
เลขคู่: 32, 10, 86, 56, 98, 82, 66 (7 จำนวน)
ต้องการคำนวณ: A
1. 32 + 10 + 86 + 56 + 98 + 82 + 66 = 430
2. 23 - 45 - 79 = -101
ใครพอจะมีตัวอย่างโปรแกรมที่คล้ายกัน หรือพอช่วยแนะนำว่าผมควรทำยังไงบ้างพอเป็นขั้นตอนจะขอบคุณมากๆครับ
Tag : - - - -

|
|
 |
 |
 |
 |
Date :
2009-07-06 03:03:57 |
By :
cassanowa |
View :
2450 |
Reply :
25 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เลขคู่ คือเลขที่ mod ด้วย 2 แล้วเท่ากับ 0
นอกนั้นเลขคี่
|
 |
 |
 |
 |
Date :
2009-07-06 12:50:51 |
By :
deawx |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนั้นพอได้ครับ ติดที่ว่าเราจะเก็บค่าเพื่อนำมาคำนวนต่อยังไงหนะครับ
|
 |
 |
 |
 |
Date :
2009-07-06 13:55:19 |
By :
cass |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สงสัยตรงที่ SWITCH-CASE เท่านั้น หมายความว่า while ใช้ไม่ได้เหรอ หรือว่า ใช้ switch-case แทน if เท่านั้น
|
 |
 |
 |
 |
Date :
2009-07-06 13:57:37 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ switch เท่านั้นครับ while for หรืออื่นๆใช้ไม่ได้ครับ
|
 |
 |
 |
 |
Date :
2009-07-06 14:29:20 |
By :
cass |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วต้องนับว่ามีกี่จำนวนอีก งงมาก ขอพี่ๆช่วยทีเถอะครับ
|
 |
 |
 |
 |
Date :
2009-07-06 14:33:10 |
By :
cassanowa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมคิว่ามันคงต้องใช้ loop บ้างนะครับ เพื่อแยกกลุ่มตัวเลข
|
 |
 |
 |
 |
Date :
2009-07-06 14:40:05 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อาจารย์เค้าไม่ให้ใช้ลูปนั้นหละครับ ปัญหาใหญ่ ผมเลยไม่รู้จะแยกกลุ่มตัวเลขยังไง แล้วจะนับยังไงอีกครับ
|
 |
 |
 |
 |
Date :
2009-07-06 14:53:03 |
By :
cassanowa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วใช้ loop แบบ array_walk() ได้ป่าวคับ 
|
 |
 |
 |
 |
Date :
2009-07-06 15:21:52 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ห้ามใช้ array ด้วยหนะครับ ผมเลยปวดหัวมากกว่าเดิมตอนนี้จะบ้าละครับ
|
 |
 |
 |
 |
Date :
2009-07-06 16:41:28 |
By :
cassanowa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
String มันก็เป็น array อยู่แล้ว นี่ครับ อาจารย์ 
|
 |
 |
 |
 |
Date :
2009-07-06 16:56:53 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือเค้าต้องการให้ใช้เงื่อนใขวัดกันตรงๆหนะครับ ช่วยทีนะครับ งานไม่เดินเลย
|
 |
 |
 |
 |
Date :
2009-07-06 17:09:15 |
By :
cassanowa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คับ ผมหาทางได้ละ แต่คิดตัง 
|
 |
 |
 |
 |
Date :
2009-07-06 17:32:43 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมก็ลุ้นอยู่นะ .....จัดสปอนเซอร์ให้คุณเอี่ยวไปเลยครับ คิดว่าคุณเอี่ยวคงมีไอเดีย
แต่ผมหล่ะ มึนๆๆงงๆๆๆ 
|
 |
 |
 |
 |
Date :
2009-07-06 19:39:26 |
By :
panyapol |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โหดจาง แนะนำหน่อยไม่ได้เหรอครับ ยังไม่ได้ทำงานไม่มีตังอะครับ
|
 |
 |
 |
 |
Date :
2009-07-06 19:47:46 |
By :
cassanowa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หาจำนวน โดยใช้ trim() และ preg_match_all()+1; หรือใช้ preg_math() แล้วตัดทิ้งทีละอัน
ใช้ switch-case ตั้งแต่ 99 - 1(จำนวน ตัวเลข)
ตัดตัวเลขออกมาหา คู่ คี่
ทำสมการทั้งหมด เก็บค่าไว้ในตัวแปร
ตัดตัวเลขและ , ทิ้งไป
ไม่ต้อง break; เพราะมันจะรันไปจนตัวสุดท้าย
โค้ดคงยาวไม่น้อย 
http://gunner.freetzi.com
|
 |
 |
 |
 |
Date :
2009-07-06 20:02:16 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ ผมพอทำได้ละ ถ้าเสร็จแล้วจะเอาโค๊ตมาแปะไว้ไห้เผื่อคนสนใจนะคับ ตอบแทนบอร์ดหน่อย
|
 |
 |
 |
 |
Date :
2009-07-06 20:58:14 |
By :
cassanowa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ปัญหาสุดท้ายละครับช่วยทีนะครับ พอจะมีฟังก์ชั่นที่ใส่สัญลักษณ์ได้มั้ยครับ
มันออกมาเป็นอย่างนี้หนะครับ
คือตัวแปลตัวไหนไม่มีค่าก็จะว่างไว้ แต่สัญลักษณ์มันยังอยู่หนะครับ
ช่วยทีนะครับพี่ๆ
โค๊ต ลิงค์โหลดนะครับ ยังไม่สมบูรณ์


ตัวอย่างตอน echo
switch($choice){
case A:
echo "1. $even1 + $even2 + $even3 + $even4 + $even5 + $even6 + $even7 + $even8 + $even9 + $even10 = ";
echo $even1 + $even2 + $even3 + $even4 + $even5 + $even6 + $even7 + $even8 + $even9 + $even10."<br>";
echo "2. $odd1 - $odd2 - $odd3 - $odd4 - $odd5 - $odd6 - $odd7 - $odd8 - $odd9 - $odd10 = ";
echo $odd1 - $odd2 - $odd3 - $odd4 - $odd5 - $odd6 - $odd7 - $odd8 - $odd9 - $odd10;
break;
}
|
 |
 |
 |
 |
Date :
2009-07-06 22:13:25 |
By :
cassanowa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ที่ผมบอก ไม่ใช่แบบนี้ 
|
 |
 |
 |
 |
Date :
2009-07-06 22:27:23 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วไม่มีวิธีใส่เครื่องหมายบ้างเลยเหรอครับ
|
 |
 |
 |
 |
Date :
2009-07-06 22:59:41 |
By :
cassanowa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
echo "\$";
|
 |
 |
 |
 |
Date :
2009-07-06 23:13:00 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาไปใช้ได้ยังไงเหรอครับ มันก็ออกมาเป็น ตัวแปลเลยไม่ต้องแสดงค่า ไม่เค้าใจจริงๆขอตัวอย่างหน่อยนะครับ จะเสร็จแล้ว ขอบคุณมากนะครับที่แนะนำ
|
 |
 |
 |
 |
Date :
2009-07-06 23:55:46 |
By :
cassanowa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ผมพยายามจะใช้ str_replace อยู่ แต่มันดันออกหลังเลขตัวสุดท้ายด้วยหนิซิครับ ปัญหาอีกละ
|
 |
 |
 |
 |
Date :
2009-07-06 23:58:27 |
By :
cassanowa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ ' แทน " คับ
|
 |
 |
 |
 |
Date :
2009-07-07 08:57:52 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งงได้อีก ขอตัวอย่างหน่อยได้มั้ยครับผมพึ่งปีเองถือว่าส่งเสริมอนาคตเด็กไทยเถอะครับ
ปัญหาที่ตรงนี้นะครับ

|
 |
 |
 |
 |
Date :
2009-07-07 22:46:07 |
By :
cassanowa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เสร็จละคับ ตามสัญญา

|
 |
 |
 |
 |
Date :
2009-07-09 13:15:34 |
By :
cassanowa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|