Re: 請教vb.net 的偵測
※ 引述《jiannrong@kkcity.com.tw ( )》之銘言:
: Dim grade() As Integer = New Integer(9) {}
: Dim i, total As Integer
: total = 0
: For i = 0 To 8
: grade(i) = InputBox("請輸入" & i + 1 & "學生成績", "共有九筆")
: 'Console.Write("請輸入{0:D2}位學生的成績:", i + 1)
: 'grade(i) = Console.ReadLine()
: total += grade(i) '計算成績總合
: Next
: MsgBox("學生總平均為" & total / 9)
: 請教一下,我如何在inputbox這裡偵測 如果輸入的不是整數 他就不會儲存到陣列裡呢
: 謝謝
把讀進來的string的每一個char用IsNumber loop一下, 不就知道了?
Overloads Public Shared Function IsNumber(String, Integer) As Boolean
Indicates whether the character at the specified position in a specified
string is categorized as a number.
懶一點的話就Try... Catch包著Convert.ToInt32()吧... 再在
Catch時做重新讀入吧... (不過Exception的處理cost有點小貴...)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 202.134.126.84
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):
Programming 近期熱門文章
PTT數位生活區 即時熱門文章