1-代码之基础语法
来源:互联网 发布:海南大学网络教育 编辑:程序博客网 时间:2024/05/16 06:26
计算每年的定存
Module Module1
Sub Main()
Dim amount, prin As Decimal
Dim rate As Double
Dim year As Integer
Dim output As String
prin = 1000.0
rate = 0.05
output = "year" & vbTab & "Amount on deposit" & vbCrLf
For year = 1 To 10
amount = prin * (1 + rate) ^ year
output &= year & vbTab & String.Format("{0:C}", amount) & vbCrLf
Next
MessageBox.Show(output, "comp interest", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
End Module
@String.Format()语法: String.Format( {index[,alignment][:formatString]} )
index--索引组件:
必选.“索引”组件(也叫参数说明符)是一个从 0 开始的数字,可标识值列表中对应的元素。也就是说,参数说明符为 0 的格式项格式化列表中的第一个值,参数说明符为 1 的格式项格式化列表中的第二个值,依次类推。 通过指定相同的参数说明符,多个格式项可以引用值列表中的同一个元素。例如,通过指定类似于“{0:X} {0:E} {0:N}”的源字符串,可以将同一个数值格式化为十六进制、科学表示法和数字格式。 每一个格式项都可以引用所有的参数。例如,如果有三个值,则可以通过指定类似于“{1} {0} {2}”的源字符串来格式化第二、第一和第三个值。格式项未引用的值会被忽略。如果参数说明符指定了超出值列表范围的项,将导致运行时异常。
alignment--对齐组件:
可选.“对齐”组件是一个带符号的整数,指示首选的格式化字段宽度。如果“对齐”值小于格式化字符串的长度,“对齐”会被忽略,并且使用格式化字符串的长度作为字段宽度。如果“对齐”为正数,字段的格式化数据为右对齐;如果“对齐”为负数,字段的格式化数据为左对齐。如果需要填充,则使用空白。如果指定“对齐”,就需要使用逗号。
formatString--格式字符串组件:
可选.“格式字符串”组件由标准或自定义格式说明符组成。如果不指定“格式字符串”,则使用常规(“G”)格式说明符。如果指定“格式说明符”,需要使用冒号。
处理顺序:
如果要格式化的值是 null(在 Visual Basic 中为 Nothing),则返回空字符串 ("")。
如果要格式化的类型实现 ICustomFormatter 接口,则调用 ICustomFormatter.Format 方法。
如果前面的步骤未格式化类型,并且该类型实现 IFormattable 接口,则调用 IFormattable.ToString 方法。
如果前面的步骤未格式化类型,则调用该类型的 ToString 方法(从 Object 类继承而来)。
前面的步骤执行完毕之后应用对齐。
代码示例:
Dim FormatString1 As String = String.Format("{0:dddd MMMM}", DateTime.Now)
Dim FormatString2 As String = DateTime.Now.ToString("dddd MMMM")
String.Format("{0:####}",123), 结果: 123
String.Format("{0,8:G} ",123), 结果: 123_ _ _ _ _ (下划线表示填充空格)
==================================================================================
标签移动效果
'tips: u need a label and 2 button
Dim step1 As Integer
Public Sub mymove() 'move
Dim disX = CInt(Rnd() * 50 * step1)
Dim disY = CInt(Rnd() * 50 * step1)
Randomize()
Label1.Location = New Point(Label1.Left + disX, Label1.Top + disY)
'cannot use move method as vb6
If Label1.Top + 1.5 * Label1.Height > Me.Height Then
step1 = -1
ElseIf Label1.Top < 0 Then
step1 = 1
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
step1 = 1 'initialization
Label1.Text = "hello world"
Timer1.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = False 'stop move
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Interval = 200 'set the interval time of timer event
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Call mymove() 'call the move function
End Sub
==================================================================================
复合运算
'tips: u will c the result in the output form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b As Single
a = 28 : b = 8
Debug.WriteLine("a=" & a & "b=" & b)
a += b
Debug.WriteLine("a+=b=" & a)
a = 28 : b = 8
a = -b
Debug.WriteLine("a-=b=" & a)
a = 28 : b = 8
a *= b
Debug.WriteLine("a*=b=" & a)
a = 28 : b = 8
a /= b
Debug.WriteLine("a/=b=" & a)
a = 28 : b = 8
a /= b
Debug.WriteLine("a/=b" & a)
End Sub
==================================================================================
日期函数
'tips: u need a label and a button
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t As Date = Now
Label1.Text = Year(t) & "年" & Month(t) & "月" _
& Microsoft.VisualBasic.Day(t) & "日" _
& Hour(t) & "点" & Minute(t) & "分" & Second(t) & "秒"
End Sub
==================================================================================
枚举类型
'tips: u need a button
Public Enum workDays
Sunday = 1
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
invalid = -1
End Enum
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyDay As workDays
MyDay = Weekday(Now()) 'use the weekday function
If MyDay = workDays.Saturday Or MyDay = workDays.Sunday Then
MsgBox("It′s the weekend.Invalid work day!", MsgBoxStyle.Critical, "alert")
Else
MsgBox("It′s a workday.To the weekend, there are " & workDays.Saturday - MyDay & " days left!", MsgBoxStyle.Information, "information")
End If
End Sub
==================================================================================
循环语句
'tips: u need a button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ch As Char
Dim msg As String
Const exitCh = "?"
Dim counter As Short = 0
msg = "enter a character:"
ch = InputBox(msg, "input")
While ch <> exitCh
counter = counter + 1
ch = InputBox$(msg)
End While
MsgBox("number of character entered:" & counter)
End Sub
==================================================================================
条件语句-绝对值
'tips: u need a button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim X, absX As Integer
Dim msg As String
X = Val(InputBox("pls input a number", "input"))
If X < 0 Then absX = -1 * X
MsgBox(X & "的绝对值: " & absX, MsgBoxStyle.Information, "result")
End Sub
==================================================================================
条件语句-计税
'tips: u need a button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim price, qty, total, tax, pay As Integer
Dim tax_rate As Single
Dim msg As String
price = Val(InputBox("pls input the price/unit", "input"))
qty = Val(InputBox("pls input the quantity:", "input"))
total = price * qty
If total > 100 Then
tax_rate = 0.05
Else
tax_rate = 0
End If
tax = total * tax_rate
pay = total + tax
msg = "price:" & vbTab & vbTab & price & vbNewLine _
& "quantity:" & vbTab & vbTab & qty & vbNewLine _
& "--------------------------------" & vbNewLine _
& "total price:" & vbTab & total & vbNewLine _
& "tax:" & vbTab & vbTab & tax & vbNewLine _
& "--------------------------------" & vbNewLine _
& "you should pay:" & vbTab & pay & vbNewLine
MsgBox(msg, MsgBoxStyle.Information, "account")
End Sub
==================================================================================
IIF
'tips: u need a button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num1, num2 As Integer
Dim result As String
Dim msg As String
num1 = InputBox("pls input number 1", "input")
num2 = InputBox("pls input number 2", "input")
result = IIf(num1 > num2, "大于", IIf(num1 = num2, "等于", "小于"))
msg = "num1=" & num1 & vbNewLine _
& "num2=" & num2 & vbNewLine _
& "=================" & vbNewLine _
& num1 & result & num2
MsgBox(msg, MsgBoxStyle.Information, "compare")
End Sub
==================================================================================
SELECT语句
'tips: u need a button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ch As Char
Dim inputStr, msg As String
Dim i As Integer
inputStr = InputBox("pls input some characters")
For i = 1 To Len(inputStr)
ch = Mid(inputStr, i, 1)
Select Case ch
Case "a" To "z", "A" To "Z"
msg &= ch & "是字母字符" & vbNewLine
Case "0" To "9"
msg &= ch + "是数字字符" & vbNewLine
Case Else
msg &= ch + "是其他字符" & vbNewLine
End Select
Next
MsgBox(msg, MsgBoxStyle.Information, "result")
End Sub
==================================================================================
SELECT语句
'tips: u need a button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim msg, userinput
userinput = InputBox("pls input a character", "input")
If Not IsNumeric(userinput) Then
If Len(userinput) <> 0 Then
Select Case Asc(userinput)
Case 65 To 90
msg = "你输入的是大写英文字母'"
msg = msg & Chr(Asc(userinput)) & "'."
Case 97 To 122
msg = "你输入的是小写英文字母'"
msg = msg & Chr(Asc(userinput)) & "'."
Case Else
msg = "你没有输入英文字母或数字字符"
End Select
End If
Else
Select Case CDbl(userinput)
Case 1, 3, 5, 6, 9
msg = userinput & "是一个奇数."
Case 0, 2, 4, 6, 8
msg = userinput & "是一个偶数."
Case Else
msg = "你输入的数字超出了"
msg = msg & "所允许的范围."
End Select
End If
MsgBox(msg, MsgBoxStyle.Information, "result")
End Sub
==================================================================================
FOR循环
'tips: u need a button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, s As Integer
Dim msg As String
s = 0
For i = 1 To 100 Step 2
s = s + i
Next i
msg &= "1+3+5+...+99=" & s
MsgBox(msg, MsgBoxStyle.Information, "result")
End Sub
==================================================================================
FOR循环--阶乘
'tips: u need a button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim n, k, i As Long
n = InputBox("enter n:")
k = 1
For i = 1 To n
k = k * i
Next i
MsgBox(Str(n) & "!=" & Str(k), MsgBoxStyle.Information, "result")
End Sub
==================================================================================
DO。。WHILE循环--求自然对数E
'tips: u need a button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i%, n&, t!, e1!
e1 = 0 : i = 0 : n = 1 : t = 1
Do While t > 0.00001
e1 = e1 + t '累加和
i = i + 1
n = n * i '连乘,求阶乘
t = 1 / n '计数第i项值
Loop
MsgBox("求e时计算了" & Str(i) & "项的和是" & Str(e1), MsgBoxStyle.Information, "result")
End Sub
==================================================================================
数组--高于平均分的人数
'tips: u need a button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Const N As Integer = 3
Dim mark(N) As Integer
Dim aver!, overn%, i%
aver = 0
For i = 1 To N
mark(i) = InputBox("输入" & i & "位学生的成绩")
aver = aver + mark(i)
Next
aver = aver / N
overn = 0
For i = 1 To N
If mark(i) > aver Then overn = overn + 1
Next
MsgBox("average mark:" & aver & ", above average:" & overn, MsgBoxStyle.Information, "result")
End Sub
==================================================================================
数组嵌套
'tips: u need a button
Dim i As Short
Dim msg As String
Static Firstarray(4) As String
For i = 0 To 4
Firstarray(i) = Chr(i + 65)
Next i
Static Secondarray(4) As Short
For i = 0 To 4
Secondarray(i) = i + 10
Next i
Static Thirdarray(1)
Thirdarray(0) = Firstarray
Thirdarray(1) = Secondarray
For i = 0 To 4
msg &= Thirdarray(0)(i) & vbTab
Next i
msg &= vbCrlf
For i = 0 To 4
msg &= Thirdarray(1)(i) & vbTab
Next i
msg &= vbCrlf
MsgBox(msg, MsgBoxStyle.Information, "result")
==================================================================================
数组函数
'tips: u need a button
Dim test(10), i As Integer
Dim msg As String
For i = 0 To 10
test(i) = i
msg &= Str(test(i)) & vbTab
Next i
Erase test 'use the erase function
msg &= vbCrlf & "erase test():" & vbCrlf
ReDim test(12)
For i = 0 To 12
msg &= Str(test(i)) & vbTab
Next i
msg &= vbCrlf
MsgBox(msg, MsgBoxStyle.Information, "result")
==================================================================================
三维数组
'tips: u need a button
Dim i, j, k, a(1, 2, 1) As Short
Dim msg As String
For i = 0 To 1
For j = 0 To 2
For k = 0 To 1
a(i, j, k) = i + j + k
Next k
Next j
Next i
For i = 0 To 1
For j = 0 To 2
For k = 0 To 1
msg &= " " & "a(" & Str(i) & "," & Str(j) & "," & Str(k) & ")=" & Str(a(i, j, k)) & vbCrlf
Next
Next
Next
MsgBox(msg, MsgBoxStyle.Information, "result")
==================================================================================
数组--for each
'tips: u need a button
Dim arr(10), Arr_elem, Sum
Dim i As Short
Dim msg As String
For i = 0 To 10
arr(i) = Int(Rnd() * 100)
Next i
For Each Arr_elem In arr
If Arr_elem > 50 Then
msg &= Str(Arr_elem) & vbTab
Sum = Sum + Arr_elem
End If
If Arr_elem > 95 Then Exit For
Next Arr_elem
msg = mid(msg,1,len(msg)-1)
MsgBox( msg & "=" & Str(Sum), MsgBoxStyle.Information, "result")
==================================================================================
数组--统计字母个数
'tips: u need a button
Dim a%(26), c$, inputStr$, le%, i%, j%, msg$
inputStr=inputbox("pls input some characters")
le = Len(inputStr) '求字符串的长度
For I = 1 To le
c = UCase(Mid(inputStr, i, 1)) '取一个字符,转换成大写
If c >= "A" And c <= "Z" Then
j = Asc(c) - 65 + 1 '将A~Z大写字母转换成1~26的下标
a(j) = a(j) + 1 '对应数组元素加1
End If
Next i
For j = 1 To 26 '输出字母及其出现的次数
If a(j) > 0 Then msg &= " " & Chr(j + 64) & "=" & a(j) & vbTab
Next j
MsgBox( "frequency of characters:" & msg, MsgBoxStyle.Information, "result")
==================================================================================
数组--分裂、合并
'tips: u need a textbox and 2 button
Dim a() As String
'输入数据,去除非法数字
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case e.KeyChar
Case "0" To "9", ",", ".", "-"
'0~9,逗号,小数点,负号为有效数字串,可以继续输入
Case Else
'输入非数字字符,去除非法字符,再输入
e.Handled =true
End Select
End Sub
'输入的内容按逗号为分隔符分离,结果放入a字符数组中
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim temp As String, i As Integer
Dim msg As String
temp = Replace(TextBox1.Text, ",,", ",") '调用Replace函数去除出现的连续分隔符
a = Split(temp, "," '调用Split函数将Text1的内容按逗号为分隔符分离,结果放入a数组中
For i = 0 To UBound(a)
msg &= a(i) & space(2)
Next i
MsgBox( msg, MsgBoxStyle.Information, "after split")
End Sub
'将数组a中各元素合并
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim msg As String
msg = Join(a, "|")
MsgBox( msg, MsgBoxStyle.Information, "after join together")
End Sub
==================================================================================
数组排序
'tips: u need a button
Dim iA%(10), iMin%, n%, i%, j%, t%, k%, msg$
iA(1) = 8 : iA(2) = 6 : iA(3) = 9 : iA(4) = 3 : iA(5) = 2 : iA(6) = 7
n = 6
For i = 1 To n - 1 '进行n-1轮比较
For j = n To i + 1 Step -1 '从n~I个元素进行两两比较
If iA(j) < iA(j - 1) Then '若次序不对,则马上进行交换位置
t = iA(j)
iA(j) = iA(j - 1)
iA(j - 1) = t
End If
Next j '出了内循环,一轮排序结束,最小数已冒到最上面
Next i
For i = 1 To 6
msg &= iA(i) & vbTab
Next i
MsgBox( msg, MsgBoxStyle.Information, "after sort")
==================================================================================
新增数组元素
'tips: u need a button
Dim a%(10), I%, k%, addnum%, msg$
For I = 1 To 9 'initialize the sorted array
a(I) = (I - 1) * 3 + 1
Next I
addnum = val(inputbox("pls input the number u wanna seek(1-25)", "input"))
For k = 1 To 9 'find the index of seeknum
If addnum < a(k) Then Exit For
Next k
For I = 9 To k Step -1 'move items
a(I + 1) = a(I)
Next I
a(k) = addnum 'insert
For I = 1 To 10
msg &= a(I) & vbTab
Next
MsgBox( msg, MsgBoxStyle.Information, "result")
==================================================================================
结构体
'tips: u need a button
Public Structure mail '定义结构类型
Public num As Short
Public name As String
Public title As String
Public addr As String
Public zip As Integer
Public tel As String
End Structure
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim member1 As mail '定义结构变量
Dim msg As String
member1.num = val(inputbox("pls input number","input")) '对结构各成员赋值
member1.name = inputbox("pls input name","input")
member1.title =inputbox("pls input title","input")
member1.addr = inputbox("pls input address","input")
member1.zip = val(inputbox("pls input zip code","input"))
member1.tel = inputbox("pls input the telphone","input")
'通过结构变量分别引用结构的各个成员
msg = "number" & vbTab & "name" & vbTab & "title" & vbTab _
& "address" & vbTab & "zip" & vbTab & "tel" & vbCrlf _
& "-----------------------------------------" _
& "-----------------------------------------" & vbCrlf _
& member1.num & vbTab & member1.name & vbTab & member1.title & vbTab _
& member1.addr & vbTab & member1.zip & vbTab & member1.tel
MsgBox( msg, MsgBoxStyle.Information, "result")
End Sub
==================================================================================
结构体数组
Const MAX_MEM = 2
Private Structure mail
Public num As Short
Public name As String
Public title As String
Public addr As String
Public zip As Integer
Public tel As String
End Structure
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim list(MAX_MEM) As mail 'define a array
Dim msg As String
Dim i As Short
'从键盘上一次录入每个会员的各数据项的数据
For i = 0 To MAX_MEM
list(i).num = val(inputbox("pls input number","input"))
list(i).name = inputbox("pls input name","input")
list(i).title =inputbox("pls input title","input")
list(i).addr = inputbox("pls input address","input")
list(i).zip = val(inputbox("pls input zip code","input"))
list(i).tel = inputbox("pls input the telphone","input")
Next i
'输出数据
msg = "number" & vbTab & "name" & vbTab & "title" & vbTab _
& "address" & vbTab & "zip" & vbTab & "tel" & vbCrlf _
& "-----------------------------------------" _
& "-----------------------------------------" & vbCrlf
'依次显示
Dim spa As String = " "
For i = 0 To MAX_MEM
msg &= list(i).num & vbTab & list(i).name & vbTab & list(i).title & vbTab _
& list(i).addr & vbTab & list(i).zip & vbTab & list(i).tel & vbCrlf
Next
MsgBox( msg, MsgBoxStyle.Information, "result")
End Sub
==================================================================================
集合
Dim i As Short
Dim msg As String
Dim myname As New Collection()
Dim x
For i = 1 To 10
myname.Add(item:="name" & i, key:="key#" & i)
msg &=myname(i) & " "
Next i
msg &= vbcrlf
myname.Remove(3)
myname.Remove("key#9")
msg &= "after delete items" & vbcrlf
For i = 1 To 8
msg &=myname(i) & " "
Next i
MsgBox( msg, MsgBoxStyle.Information, "result")
==================================================================================
集合
Dim anytype As New Collection()
Dim x, a As Object
x = "Visual Basic.NET"
anytype.Add(x)
x = 246
anytype.Add(x)
anytype.Add(Now)
anytype.Add(9876.5436)
anytype.Add("微型计算机")
For Each a In anytype
msgbox(a)
Next a
- 1-代码之基础语法
- HTML+CSS基础课程之CSS基础代码语法
- php基础语法 代码
- Scala 基础语法代码
- 代码干货 | Javascript基础学习之基本语法
- CSS之基础语法
- java基础之语法
- javascript101之语法基础
- J2SE之基础语法
- Lua基础之语法
- Java之语法基础
- Java基础之语法
- Java基础之语法
- JavaScript基础之语法
- C#之基础语法
- JavaScript基础之语法
- JAVA基础 之 语法基础
- Java基础之基础语法
- 全面理解COM+ →COM+系统服务介绍
- Linux SHELL if 命令参数说明
- C/C++中的日期和时间
- HIS系统实施的目的和目标
- 比较
- 1-代码之基础语法
- 一个动态链接的内存管理问题
- DotNetBar 6.5.0.3 for VS2003
- 《深入解析Windows 操作系统,第4版》勘误 更新日期:2007年04月22日
- dotnetCharting 4.2 for VS2003
- JS 切换图片 (简单模式) 字体颜色,大小变化
- 数字压缩编码
- 《给初学者的Windows Vista的补遗手册》之083
- 修炼中...