[.NET] 數值判斷勝負的地方會有小問題

看板Visual_Basic作者 (人緣有待等)時間16年前 (2009/06/14 22:41), 編輯推噓0(005)
留言5則, 2人參與, 最新討論串1/1
請輸入專案類型(網站專案或者應用程式專案):VB2008 a = Val(Label2.Text) If a > 10 Then a = 0.5 End If sum = a If sum >= 8 And sum <= 10 And sum > sumI Then MsgBox("你輸了") Button2.Enabled = False ElseIf sum >= 8 And sum <= 10 And sum = sumI Then MsgBox("平手") Button2.Enabled = False ElseIf sum >= 8 And sum <= 10 And sum < sumI Then MsgBox("你贏了") Button2.Enabled = False End If If a < 8 Then Label3.Text = CInt(Int((13 - 1 + 1) * Rnd() + 1)) b = Val(Label3.Text) If b > 10 Then b = 0.5 End If sum = a + b If sum > 10.5 Then MsgBox("你贏了") Button2.Enabled = False End If If sum >= 8 And sum <= 10.5 And sum > sumI Then MsgBox("你輸了") Button2.Enabled = False ElseIf sum >= 8 And sum <= 10.5 And sum < sumI Then MsgBox("你贏了") Button2.Enabled = False ElseIf sum >= 8 And sum <= 10.5 And sum = sumI Then MsgBox("平手") Button2.Enabled = False End If If a + b < 8 Then Label4.Enabled = True Label4.Text = CInt(Int((13 - 1 + 1) * Rnd() + 1)) c = Val(Label4.Text) If c > 10 Then c = 0.5 End If sum = a + b + c If sum > 10.5 Then MsgBox("你贏了") Button2.Enabled = False End If If sum >= 8 And sum <= 10.5 And sum > sumI Then MsgBox("你輸了") Button2.Enabled = False ElseIf sum >= 8 And sum <= 10.5 And sum < sumI Then MsgBox("你贏了") Button2.Enabled = False ElseIf sum >= 8 And sum <= 10.5 And sum = sumI Then MsgBox("平手") Button2.Enabled = False End If If a + b + c < 8 Then Label5.Enabled = True Label5.Text = CInt(Int((13 - 1 + 1) * Rnd() + 1)) d = Val(Label5.Text) If d > 10 Then d = 0.5 End If sum = a + b + c + d If sum > 10.5 Then MsgBox("你贏了") Button2.Enabled = False End If If sum >= 8 And sum <= 10.5 And sum > sumI Then MsgBox("你輸了") Button2.Enabled = False ElseIf sum >= 8 And sum <= 10.5 And sum < sumI Then MsgBox("你贏了") Button2.Enabled = False ElseIf sum >= 8 And sum <= 10.5 And sum = sumI Then MsgBox("平手") Button2.Enabled = False End If If a + b + c + d < 8 Then Label6.Enabled = True Label6.Text = CInt(Int((13 - 1 + 1) * Rnd() + 1)) f = Val(Label6.Text) If f > 10 Then f = 0.5 End If sum = a + b + c + d + f If sum >= 10.5 Then MsgBox("你贏了") Button2.Enabled = False End If If sum >= 8 And sum <= 10.5 And sum > sumI Then MsgBox("你輸了") Button2.Enabled = False ElseIf sum >= 8 And sum <= 10.5 And sum < sumI Then MsgBox("你贏了") Button2.Enabled = False ElseIf sum >= 8 And sum <= 10.5 And sum = sumI Then MsgBox("平手") Button2.Enabled = False End If End If End If End If End If End Sub 備註:當中的sumI是玩家的總值 自己在寫一個十點半的小遊戲 上面是電腦判斷勝負的過程 在測試的時候 發覺有時候明明電腦的總值比玩家大 卻會顯示出"你贏了"而不是"你輸了"(總值比玩家小也有這情況~"~) 有時候雙方平手卻顯示"你贏了"...囧 在判斷大小的地方覺得沒有錯誤 卻始終找不出原因~"~ 想請問一下究竟是那個部分的判斷有出錯?? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.38.8.85 ※ 編輯: j76992001 來自: 114.38.8.85 (06/14 22:42)

06/15 02:06, , 1F
差不多的判斷寫四五次,這樣不搞出問題也難。
06/15 02:06, 1F

06/15 02:07, , 2F
每次判斷做的事情都差不多,那就不如取出來變成一個函數
06/15 02:07, 2F

06/15 02:07, , 3F
每次有輸入之後先讀取輸入,然後叫這個函數。
06/15 02:07, 3F

06/15 02:08, , 4F
不然我實在無法提起勁看你是錯在哪裡。可能別人可以吧。
06/15 02:08, 4F

06/15 08:34, , 5F
恩恩 感謝MOONRAKER大提供的意見~"~
06/15 08:34, 5F
文章代碼(AID): #1ADGmJG4 (Visual_Basic)