สุ ด ห ล่ อ ™
คนรักเสียว
พลังน้ำใจ: 5
ออฟไลน์
กระทู้: 193
|
 |
« เมื่อ: 05 สิงหาคม 2010, 23:24:24 » |
|
ผมเขียน Code สุ่ม Operator ทางคณิตศาสตร์พวก + - * / แต่มันทำไม่ได้ครับ ไม่ทราบว่าจะทำยังไงดี ตอนแรกสุ่มตัวเลขออกมาก่อน ถ้าเป็น 0 จะได้เครื่องหมาย Operator เป็น + ถ้าเป็น 1 เป็น - ถ้าเป็น 2 เป็น * ถ้าเป็น 3 เป็น / โดยแทนลงไปใน oper พอมาถึงบรรทัด result ไม่รู้ว่าจะเอา oper มาแทนยังไง โดยตอนนี้ผมแทนเป็น  นะครับ จะได้ไม่ งง ผมลองแทน  เป็น & oper & หรือ + oper + ก็ใช้ไม่ได้เลย เลยไม่รู้แล้วละครับว่าจะทำยังไง ใครพอทำได้ช่วยหน่อยนะครับ ขอบคุณครับ ข้างล่างนี้เป็น Code ของผมนะครับ Dim oper As Char Dim result As String If Int(Rnd() * 4) = 0 Then oper = "+" ElseIf Int(Rnd() * 4) = 1 Then oper = "-" ElseIf Int(Rnd() * 4) = 2 Then oper = "*" ElseIf Int(Rnd() * 4) = 3 Then oper = "/" End If result = Val(Label2.Text)  Val(Label3.Text)  Val(Label4.Text)  Val(Label5.Text)  Val(Label6.Text)
|
|
|
บันทึกการเข้า
|
|
|
|
eable
Newbie
พลังน้ำใจ: 5
ออฟไลน์
กระทู้: 52
|
 |
« ตอบ #1 เมื่อ: 06 สิงหาคม 2010, 00:26:43 » |
|
1. Dim oper As Char - เปลี่ยนเป็น Dim oper As String เพราะ Char ไม่มีใน VB
2. result = Val(Label2.Text) ... - เอา Val ออก เพราะ result เป็น string แต่ Val() เป็น numeric และ... - เปลี่ยน .Text เป็น .Caption เพราะ .Text ไม่มีนะ สำหรับ label (ถ้าจำไม่ผิด)
3. ใช้ +oper+
|
|
|
บันทึกการเข้า
|
|
|
|
MrWebmonster
หัวหน้าแก๊งเสียว
พลังน้ำใจ: 124
ออฟไลน์
กระทู้: 2,922
|
 |
« ตอบ #2 เมื่อ: 06 สิงหาคม 2010, 00:38:51 » |
|
ตามด้านบนบอกครับ
ตามข้อ 3 จะใช้ +oper+ หรือ &oper& ก็ได้ ไม่น่ามีปัญหา เพราะว่าเป็นการ operation ระหว่าง string
|
|
|
บันทึกการเข้า
|
|
|
|
สุ ด ห ล่ อ ™
คนรักเสียว
พลังน้ำใจ: 5
ออฟไลน์
กระทู้: 193
|
 |
« ตอบ #3 เมื่อ: 06 สิงหาคม 2010, 07:18:47 » |
|
ผมลองเปลี่ยนมาใช้ .Caption แต่ก็ไม่ได้ครับ มันบอก่า Warning 1 Variable 'oper' is used before it has been assigned a value. A null reference exception could result at runtime แต่ถ้าใช้ .Text เวลาแสดงผลออกมา ผมลองจับใส่ใน MsgBox(result)แสดงผลเป็น 7+5 แบบเนี้ยอะครับ มันไม่ใช่คำตอบเป็นเลข 12  Dim oper As String Dim result As String
If Int(Rnd() * 4) = 0 Then oper = "+" ElseIf Int(Rnd() * 4) = 1 Then oper = "-" ElseIf Int(Rnd() * 4) = 2 Then oper = "*" ElseIf Int(Rnd() * 4) = 3 Then oper = "/" End If
result = Label2.Text & oper & Label3.Text MsgBox(result)
|
|
|
บันทึกการเข้า
|
|
|
|
in2clearsky
ก๊วนเสียว
พลังน้ำใจ: 44
ออฟไลน์
กระทู้: 375
|
 |
« ตอบ #4 เมื่อ: 06 สิงหาคม 2010, 09:24:01 » |
|
Dim oper As String Dim result As String Dim randNo As Integer randNo = Int(Rnd() * 4) If randNo = 0 Then oper = "+" ElseIf randNo = 1 Then oper = "-" ElseIf randNo = 2 Then oper = "*" ElseIf randNo = 3 Then oper = "/" End If
result = Label2.Text & oper & Label3.Text MsgBox(result)
แก้ตามนี้ครับ การใช้ Rnd() ใน if จะเป็นการ random ทุกครั้งก่อนที่จะ if ทำให้ค่าเปลี่ยนทุกครั้ง ดังนั้นควรจะ random ทีเดียวแล้วเก็บไว้ในตัวแปรก่อนครับ
|
|
|
บันทึกการเข้า
|
Dirty Deeds Done Dirt Cheap
|
|
|
iampum
สมุนแก๊งเสียว
พลังน้ำใจ: 157
ออฟไลน์
กระทู้: 524
|
 |
« ตอบ #5 เมื่อ: 06 สิงหาคม 2010, 09:39:06 » |
|
สู้ๆ 
|
|
|
บันทึกการเข้า
|
สีพ่นซ่อมเฉพาะจุดงานพ่นสีเหล็กชุบ EDP, พ่นซ่อมเฉพาะจุดสี powder coating ,งานฉีดพลาสติกประเภท PP,ABS,POM,PE,PVC,งานเบาะหนัง, Anti-rust spray, Mold cleaner, Release agent, Multi-Purpose, Spatter, sticker & adhesive Remover 
|
|
|
cnx42
Verified Seller
สมุนแก๊งเสียว
พลังน้ำใจ: 207
ออฟไลน์
กระทู้: 615
|
 |
« ตอบ #6 เมื่อ: 06 สิงหาคม 2010, 10:05:08 » |
|
ไม่รู้ใช่ไหม
Private Sub Command1_Click() MsgBox getoperand End Sub
Function getoperand() As String Dim randNo As Integer randNo = Int(Rnd() * 4) Select Case randNo Case 1 getoperand = "+" Case 2 getoperand = "-" Case 3 getoperand = "*" Case 4 getoperand = "/" End Select End Function
|
|
|
บันทึกการเข้า
|
|
|
|
@@@
Administrator
สมุนแก๊งเสียว
พลังน้ำใจ: 33
ออฟไลน์
กระทู้: 634
|
 |
« ตอบ #7 เมื่อ: 06 สิงหาคม 2010, 10:13:00 » |
|
เป็นผมจะคิดง่ายแบบชาวนา  Dim oper As String Dim result As String Dim randNo As Integer randNo = Int(Rnd() * 4) If randNo = 0 Then result = Label2.Text + Label3.Text ElseIf randNo = 1 Then result = Label2.Text - Label3.Text ElseIf randNo = 2 Then result = Label2.Text * Label3.Text ElseIf randNo = 3 Then result = Label2.Text / Label3.Text End If
MsgBox(result)
|
|
|
บันทึกการเข้า
|
รับซื้อเว็บ 100uip ต่อวันขึ้นไป EA Forex 
|
|
|
MrWebmonster
หัวหน้าแก๊งเสียว
พลังน้ำใจ: 124
ออฟไลน์
กระทู้: 2,922
|
 |
« ตอบ #8 เมื่อ: 06 สิงหาคม 2010, 10:21:06 » |
|
^ ^ ^ ว่าจะมาบอกคล้ายๆ ด้านบน Dim oper As String ' กำหนดเป็น Integer เพราะจะเอาตัวเลขมา Operation กัน Dim result As Integer ' สมมติ ว่า Fix ค่า ลงไป ซึ่ง Label2 กับ Label2 ต้องมีลากวางไว้บน ฟอร์ม Label2.Caption = 7 Label3.Caption = 5
If Int(Rnd() * 4) = 0 Then oper = "+" ElseIf Int(Rnd() * 4) = 1 Then oper = "-" ElseIf Int(Rnd() * 4) = 2 Then oper = "*" ElseIf Int(Rnd() * 4) = 3 Then oper = "/" End If
Select Case oper Case "+": result = Val(Label2.Caption) + Val(Label3.Caption) Case "-": result = Val(Label2.Caption) - Val(Label3.Caption) Case "*": result = Val(Label2.Caption) * Val(Label3.Caption) Case "/": result = Val(Label2.Caption) / Val(Label3.Caption) Case Else: result = 0 End Select
MsgBox (result)
|
|
|
บันทึกการเข้า
|
|
|
|
eable
Newbie
พลังน้ำใจ: 5
ออฟไลน์
กระทู้: 52
|
 |
« ตอบ #9 เมื่อ: 06 สิงหาคม 2010, 10:56:50 » |
|
 นึกว่าอยากได้ผลลัพธ์ที่เป็น text แบบ "4*5" อะไรแบบนี้ เห็นกำหนด result เป็น string
|
|
|
บันทึกการเข้า
|
|
|
|
สุ ด ห ล่ อ ™
คนรักเสียว
พลังน้ำใจ: 5
ออฟไลน์
กระทู้: 193
|
 |
« ตอบ #10 เมื่อ: 06 สิงหาคม 2010, 16:45:51 » |
|
^ ^ ^ ว่าจะมาบอกคล้ายๆ ด้านบน Dim oper As String ' กำหนดเป็น Integer เพราะจะเอาตัวเลขมา Operation กัน Dim result As Integer ' สมมติ ว่า Fix ค่า ลงไป ซึ่ง Label2 กับ Label2 ต้องมีลากวางไว้บน ฟอร์ม Label2.Caption = 7 Label3.Caption = 5
If Int(Rnd() * 4) = 0 Then oper = "+" ElseIf Int(Rnd() * 4) = 1 Then oper = "-" ElseIf Int(Rnd() * 4) = 2 Then oper = "*" ElseIf Int(Rnd() * 4) = 3 Then oper = "/" End If
Select Case oper Case "+": result = Val(Label2.Caption) + Val(Label3.Caption) Case "-": result = Val(Label2.Caption) - Val(Label3.Caption) Case "*": result = Val(Label2.Caption) * Val(Label3.Caption) Case "/": result = Val(Label2.Caption) / Val(Label3.Caption) Case Else: result = 0 End Select
MsgBox (result) ตอนแรกผมก็กะจะทำแบบนี้ แต่ว่า ผมต้องการ + - * / กับตัวเลขทั้งหมด 5 ตัวอะครับ ถ้าทำแบบนี้เขียนยาวมากๆ 
|
|
« แก้ไขครั้งสุดท้าย: 06 สิงหาคม 2010, 16:46:24 โดย สุ ด ห ล่ อ ™ »
|
บันทึกการเข้า
|
|
|
|
hero_tor
คนรักเสียว
พลังน้ำใจ: 11
ออฟไลน์
กระทู้: 143
|
 |
« ตอบ #11 เมื่อ: 06 สิงหาคม 2010, 21:03:13 » |
|
จะคำนวน expression ที่เป็น string อยู่ใช่ป่ะครับ ลอง search google คำว่า "C# Math Expression" มีหลายวิธีครับ นี่ก็อันนึงลองดูครับ ใช้วิธี คอมไฟล์ string ตอน runtime ครับ http://www.codeproject.com/KB/recipes/matheval.aspx 
|
|
|
บันทึกการเข้า
|
|
|
|
Extra Cash
สมุนแก๊งเสียว
พลังน้ำใจ: 268
ออฟไลน์
กระทู้: 631
|
 |
« ตอบ #12 เมื่อ: 06 สิงหาคม 2010, 22:22:56 » |
|
ลองใช้โค๊ดนี้ครับ ให้สร้าง textbox เป็น array ในนี้ใช้ชื่อ text(0), text(1), text(2), text(3), text(4) Dim i As Integer Dim o As Integer Dim s As String Dim result As Double s = label(0).Text result = Val(label(0).Text) For i = 1 To 4 o = Int(Rnd() * 4) Select Case o Case 0: result = result + Val(label(i).Text) Case 1: result = result - Val(label(i).Text) Case 2: result = result * Val(label(i).Text) Case 3: result = result / Val(label(i).Text) End Select s = s & " " & Mid("+-*/", o + 1, 1) & " " & label(i).Text Next MsgBox(s & " = " & Str(result))
ถ้าต้องการ ค่าจากการคำนวนอย่งเดียวก็ ตัดตัวแปร s ออกครับ
|
|
|
บันทึกการเข้า
|
|
|
|
หมูทอด
สมุนแก๊งเสียว
พลังน้ำใจ: 58
ออฟไลน์
กระทู้: 982
|
 |
« ตอบ #13 เมื่อ: 06 สิงหาคม 2010, 22:37:00 » |
|
มาดูด้วยย
|
|
|
บันทึกการเข้า
|
|
|
|
Extra Cash
สมุนแก๊งเสียว
พลังน้ำใจ: 268
ออฟไลน์
กระทู้: 631
|
 |
« ตอบ #14 เมื่อ: 08 สิงหาคม 2010, 20:27:19 » |
|
โค๊ดข้างบนมันใช้ไม้มั้ยเนี่ยท่าน สุ ด ห ล่ อ ™
|
|
|
บันทึกการเข้า
|
|
|
|
สุ ด ห ล่ อ ™
คนรักเสียว
พลังน้ำใจ: 5
ออฟไลน์
กระทู้: 193
|
 |
« ตอบ #15 เมื่อ: 08 สิงหาคม 2010, 20:58:50 » |
|
โค๊ดข้างบนมันใช้ไม้มั้ยเนี่ยท่าน สุ ด ห ล่ อ ™
ขอประธานโทษครับ คือผมลืมไปเลยว่าตั้งกระทู้ไว้ ขอลองสักครู่นะครับ 
|
|
|
บันทึกการเข้า
|
|
|
|
สุ ด ห ล่ อ ™
คนรักเสียว
พลังน้ำใจ: 5
ออฟไลน์
กระทู้: 193
|
 |
« ตอบ #16 เมื่อ: 08 สิงหาคม 2010, 21:14:52 » |
|
โค๊ดข้างบนมันใช้ไม้มั้ยเนี่ยท่าน สุ ด ห ล่ อ ™
ผมลองแล้ว คำตอบมันออกมาไม่ตรงครับ แต่ว่ายังไงก็ขอบคุณมากครับ ตอนแรกผมกะจะทำ 180IQ แบบคำตอบออกมาคงตัว คือคำตอบนั้นมีอยู่จริง แต่ตอนนี้ผมทำให้มันสุ่มคำตอบออกมาเลยครับ เป็นการตัดปัญหา 
|
|
|
บันทึกการเข้า
|
|
|
|
Extra Cash
สมุนแก๊งเสียว
พลังน้ำใจ: 268
ออฟไลน์
กระทู้: 631
|
 |
« ตอบ #17 เมื่อ: 08 สิงหาคม 2010, 21:25:55 » |
|
อย่างนี้ไปตั้งโฮสได้แล้วครับ รับผิดชอบดีไม่ทิ้งปัญหา อิอิ
|
|
|
บันทึกการเข้า
|
|
|
|
scrop
Newbie
พลังน้ำใจ: 3
ออฟไลน์
กระทู้: 29
|
 |
« ตอบ #18 เมื่อ: 08 สิงหาคม 2010, 22:08:15 » |
|
Dim dataInput(0 To 4) As Integer Dim operText(0 To 3) As String Dim operIndex As Integer Dim strResult As String
operText(0) = "+" operText(1) = "-" operText(2) = "*" operText(3) = "/"
dataInput(0) = Val(TextBox1.Text) dataInput(1) = Val(TextBox2.Text) dataInput(2) = Val(TextBox3.Text) dataInput(3) = Val(TextBox4.Text) dataInput(4) = Val(TextBox5.Text)
strResult = "((((" & dataInput(0) & " " For i = 1 To 4 strResult &= operText(Int(Rnd() * 4)) & " " & dataInput(i) & ")" Next MsgBox(strResult)
Dim result = New DataTable().Compute(strResult, Nothing)
MsgBox(strResult & " : " & result)
พออ่านดูแล้วท่าทางจะสนุก ก็เลยลองเขียนโปรแกรมขึ้นมา ไม่รู้ว่าจะตอบความตั้งใจแรกได้อยู่หรือเปล่าครับ
|
|
|
บันทึกการเข้า
|
|
|
|
สุ ด ห ล่ อ ™
คนรักเสียว
พลังน้ำใจ: 5
ออฟไลน์
กระทู้: 193
|
 |
« ตอบ #19 เมื่อ: 08 สิงหาคม 2010, 22:51:27 » |
|
Dim dataInput(0 To 4) As Integer Dim operText(0 To 3) As String Dim operIndex As Integer Dim strResult As String
operText(0) = "+" operText(1) = "-" operText(2) = "*" operText(3) = "/"
dataInput(0) = Val(TextBox1.Text) dataInput(1) = Val(TextBox2.Text) dataInput(2) = Val(TextBox3.Text) dataInput(3) = Val(TextBox4.Text) dataInput(4) = Val(TextBox5.Text)
strResult = "((((" & dataInput(0) & " " For i = 1 To 4 strResult &= operText(Int(Rnd() * 4)) & " " & dataInput(i) & ")" Next MsgBox(strResult)
Dim result = New DataTable().Compute(strResult, Nothing)
MsgBox(strResult & " : " & result)
พออ่านดูแล้วท่าทางจะสนุก ก็เลยลองเขียนโปรแกรมขึ้นมา ไม่รู้ว่าจะตอบความตั้งใจแรกได้อยู่หรือเปล่าครับ ได้แล้วครับ ขอบคุณมากครับ ^^ +1 ให้แล้วครับ
|
|
« แก้ไขครั้งสุดท้าย: 08 สิงหาคม 2010, 22:51:44 โดย สุ ด ห ล่ อ ™ »
|
บันทึกการเข้า
|
|
|
|
|