เรียนเทพ VB เข้ามาช่วยหน่อยครับ

เริ่มโดย สุ ด ห ล่ อ ™, 05 สิงหาคม 2010, 23:24:24

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

สุ ด ห ล่ อ ™

ผมเขียน 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

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

ตามด้านบนบอกครับ

ตามข้อ 3 จะใช้ +oper+ หรือ &oper& ก็ได้ ไม่น่ามีปัญหา เพราะว่าเป็นการ operation ระหว่าง string

รวมเรื่อง สัพเพเหระ ไอที [direct=https://webmonsterth.blogspot.com/]แก้ปัญหาไวรัส[/direct] คอมพิวเตอร์ทิป อินเตอร์เน็ตทิป โค้ด รหัส Ascii [direct=http://www.ezsymbols.com/]สัญลักษณ์facebook[/direct] Messenger LINE Gplus Instagram [direct=http://www.ilovebrowser.com/]แก้ปัญหาเว็บเบราเซอร์[/direct] สู้ๆ น้าาาาา ~

สุ ด ห ล่ อ ™

ผมลองเปลี่ยนมาใช้ .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  :P
อ้างถึง
        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

อ้างถึง
       
        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

[direct=https://www.fmm.co.th]สีพ่นซ่อมเฉพาะจุดงานพ่นสีเหล็กชุบ EDP, พ่นซ่อมเฉพาะจุดสี powder coating ,งานฉีดพลาสติกประเภท PP,ABS,POM,PE,PVC,งานเบาะหนัง, Anti-rust spray, Mold cleaner, Release agent, Multi-Purpose, Spatter, sticker & adhesive Remover[/direct]

cnx42

ไม่รู้ใช่ไหม

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
🔗 แลกลิงก์ เยี่ยมลิงก์ เพิ่มยอดวิว & Engagement Shopee | Lazada | YouTube | Facebook | TikTok และอื่น ๆ https://followerboostpro.com/
🧠 จินตคณิตอาวบาคัส เสริมทักษะสมองเด็ก https://owlbacus.com/
🧬 ปลุกพลังระดับเซลล์ ด้วยราชันแห่งสารต้านอนุมูลอิสระ https://medical-astareal.com/
🤖 แชทบอท AI ช่วยปิดการขาย อัตโนมัติ ขายอะไรก็ปิดดีลได้! https://saleagentpro.com/
🌿 ฟื้นบำรุงเส้นผม ด้วยโรสแมรี่ออร์แกนิก ผมแข็งแรง ลดหลุดร่วงอย่างเป็นธรรมชาติ https://verdanthair.com

@@@

เป็นผมจะคิดง่ายแบบชาวนา  :wanwan002:


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 ต่อวันขึ้นไป [direct=http://www.rommit.com/forex]EA Forex[/direct]

MrWebmonster

^
^
^

ว่าจะมาบอกคล้ายๆ ด้านบน

        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)
รวมเรื่อง สัพเพเหระ ไอที [direct=https://webmonsterth.blogspot.com/]แก้ปัญหาไวรัส[/direct] คอมพิวเตอร์ทิป อินเตอร์เน็ตทิป โค้ด รหัส Ascii [direct=http://www.ezsymbols.com/]สัญลักษณ์facebook[/direct] Messenger LINE Gplus Instagram [direct=http://www.ilovebrowser.com/]แก้ปัญหาเว็บเบราเซอร์[/direct] สู้ๆ น้าาาาา ~

eable

 :wanwan019:

นึกว่าอยากได้ผลลัพธ์ที่เป็น text แบบ "4*5" อะไรแบบนี้
เห็นกำหนด result เป็น string

สุ ด ห ล่ อ ™

#10
อ้างถึงจาก: MrWebmonster ใน 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)


ตอนแรกผมก็กะจะทำแบบนี้ แต่ว่า ผมต้องการ + - * / กับตัวเลขทั้งหมด 5 ตัวอะครับ ถ้าทำแบบนี้เขียนยาวมากๆ  :P

hero_tor

จะคำนวน expression ที่เป็น string อยู่ใช่ป่ะครับ
ลอง search google คำว่า "C# Math Expression" มีหลายวิธีครับ

นี่ก็อันนึงลองดูครับ ใช้วิธี คอมไฟล์ string ตอน runtime ครับ
http://www.codeproject.com/KB/recipes/matheval.aspx  :wanwan007:

[direct=https://www.facebook.com/akicraft.th/]Aki Craft งานปั้นตุ๊กตา ของขวัญ ด้วยดินเกาหลี
#ของขวัญวันเกิด #ของขวัญที่ไม่ซ้ำใคร[/direct]
[direct=http://akicraftth.blogspot.com/]รับปั้นตุ๊กตา ปั้นโมเดล ของขวัญวันเกิด ของขวัญวันรับปริญญา ของขวัญวันวาเลนไทน์[/direct]

Extra Cash

ลองใช้โค๊ดนี้ครับ
ให้สร้าง 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 ออกครับ

[direct=http://www.prodipper.com]PRO DIPPER
ตัวแทนจำหน่าย สีลอกได้ Plasti Dip แท้
อย่างเป็นทางการจาก Performix อเมริกา
[/direct]
       [direct=http://www.votejung.com]โหวดจัง
       โปรโมทเว็บไซต์ฟรี![/direct]
       [direct=http://fbfansmaster.com]FB FANS MASTER
       Buy Facebook Likes[/direct]


Extra Cash

โค๊ดข้างบนมันใช้ไม้มั้ยเนี่ยท่าน สุ ด ห ล่ อ ™

[direct=http://www.prodipper.com]PRO DIPPER
ตัวแทนจำหน่าย สีลอกได้ Plasti Dip แท้
อย่างเป็นทางการจาก Performix อเมริกา
[/direct]
       [direct=http://www.votejung.com]โหวดจัง
       โปรโมทเว็บไซต์ฟรี![/direct]
       [direct=http://fbfansmaster.com]FB FANS MASTER
       Buy Facebook Likes[/direct]

สุ ด ห ล่ อ ™

อ้างถึงจาก: Extra Cash ใน 08 สิงหาคม 2010, 20:27:19
โค๊ดข้างบนมันใช้ไม้มั้ยเนี่ยท่าน สุ ด ห ล่ อ ™


ขอประธานโทษครับ คือผมลืมไปเลยว่าตั้งกระทู้ไว้ ขอลองสักครู่นะครับ  :P

สุ ด ห ล่ อ ™

อ้างถึงจาก: Extra Cash ใน 08 สิงหาคม 2010, 20:27:19
โค๊ดข้างบนมันใช้ไม้มั้ยเนี่ยท่าน สุ ด ห ล่ อ ™


ผมลองแล้ว คำตอบมันออกมาไม่ตรงครับ แต่ว่ายังไงก็ขอบคุณมากครับ
ตอนแรกผมกะจะทำ 180IQ แบบคำตอบออกมาคงตัว คือคำตอบนั้นมีอยู่จริง
แต่ตอนนี้ผมทำให้มันสุ่มคำตอบออกมาเลยครับ เป็นการตัดปัญหา  :P

Extra Cash

อย่างนี้ไปตั้งโฮสได้แล้วครับ รับผิดชอบดีไม่ทิ้งปัญหา อิอิ

[direct=http://www.prodipper.com]PRO DIPPER
ตัวแทนจำหน่าย สีลอกได้ Plasti Dip แท้
อย่างเป็นทางการจาก Performix อเมริกา
[/direct]
       [direct=http://www.votejung.com]โหวดจัง
       โปรโมทเว็บไซต์ฟรี![/direct]
       [direct=http://fbfansmaster.com]FB FANS MASTER
       Buy Facebook Likes[/direct]

scrop


        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)


พออ่านดูแล้วท่าทางจะสนุก ก็เลยลองเขียนโปรแกรมขึ้นมา ไม่รู้ว่าจะตอบความตั้งใจแรกได้อยู่หรือเปล่าครับ

สุ ด ห ล่ อ ™

#19
อ้างถึงจาก: scrop ใน 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)


พออ่านดูแล้วท่าทางจะสนุก ก็เลยลองเขียนโปรแกรมขึ้นมา ไม่รู้ว่าจะตอบความตั้งใจแรกได้อยู่หรือเปล่าครับ

ได้แล้วครับ ขอบคุณมากครับ ^^
+1 ให้แล้วครับ