Crystal 發問時間： 電腦與網際網路程式設計 · 1 0 年前

EXCUSE ME

Sub string_fucntion()

Dim test_str1 As Integer

Dim test_str2 As Integer

Dim test_str3 As String

Dim temp As Integer

Do

Cells(2, 2) = "您輸入的第一個數為"

Cells(3, 2) = "您輸入的第二個數為"

Cells(2, 3) = test_str1

Cells(3, 3) = test_str2

Cells(5, 2) = "大數"

Cells(6, 2) = "小數"

Cells(8, 2) = "交換後第一個數是"

Cells(9, 2) = "交換後第二個數是"

If test_str1 < test_str2 Then

Cells(5, 3) = test_str2

Cells(6, 3) = test_str1

ElseIf test_str1 > test_str2 Then

Cells(5, 3) = test_str1

Cells(6, 3) = test_str2

End If

temp = Cells(5, 3)

Cells(8, 3) = Cells(5, 3)

Cells(5, 3) = Cells(6, 3)

Cells(9, 3) = Cells(6, 3)

Cells(6, 3) = temp

test_str3 = Application.InputBox(prompt:="really want to play again?please input Y or y,N or n", Title:="do you want to play again")

Loop While test_str3 = "Y" Or test_str3 = "y"

End Sub

2 個已更新項目:

3 個解答

• 1 0 年前
最佳解答

Sub string_fucntion()

Dim test_1 As Integer

Dim test_2 As Integer

Dim test_str3 As String

Dim temp As Integer

Do

Cells(2, 2) = "您輸入的第一個數為"

Cells(3, 2) = "您輸入的第二個數為"

Cells(2, 3) = test_1

Cells(3, 3) = test_2

Cells(5, 2) = "大數"

Cells(6, 2) = "小數"

Cells(8, 2) = "交換後第一個數是"

Cells(9, 2) = "交換後第二個數是"

If test_1 < test_2 Then

Cells(5, 3) = test_2

Cells(6, 3) = test_1

ElseIf test_1 > test_2 Then

Cells(5, 3) = test_1

Cells(6, 3) = test_2

End If

temp = test_1

test_1 = test_2

test_2 = temp

Cells(8, 3) = test_1

Cells(9, 3) = test_2

test_str3 = InputBox(prompt:="really want to play again?please input Y or y,N or n", Title:="do you want to play again")

Loop While test_str3 = "Y" Or test_str3 = "y"

End Sub

'說明:

'1. test_str1 和 test_str2 宣告為整數, 但名稱內含"str", 會讓人誤以為是字串變數, 所以我把它改掉.

'2. InputBox的傳回值是字串, 我用Val()函數將它轉為數值, 再存入變數.

'3. 你原先比較大小的地方是正確的, 但被後來的交換動作破壞掉.

'4. 我將你交換的那一段改寫, 就沒問題了.

• 1 0 年前

也謝謝另一位的回答唷~~

• 振輔
Lv 5
1 0 年前

1.程式沒有輸出敘述，看不出來對或錯。

2.內部有迴圈 do... loop，所以數值一直被重複蓋掉，就算是Functionu也只會回傳最後一次的答案。

3.在這裡家加兩個msgbox就會發現並沒有判斷錯誤啊！

MsgBox (cells(5, 3) & " > " & cells(6, 3))

temp = cells(5, 3)

cells(8, 3) = cells(5, 3)

cells(5, 3) = cells(6, 3)

cells(9, 3) = cells(6, 3)

cells(6, 3) = temp

MsgBox ("換成 " & cells(5, 3) & " < " & cells(6, 3))

2007-06-07 21:12:26 補充：

同意 東邪無弓 所說

1.其實因為你最後進行了交換,

所以我用兩個msgbox來顯示交換前跟交換後.

2.val( )的用法會比較適當