Imports System
堅(jiān)守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價(jià)值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都塑料袋小微創(chuàng)業(yè)公司專業(yè)提供成都定制網(wǎng)頁設(shè)計(jì)營銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺設(shè)計(jì)、底層架構(gòu)、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。
Module Program
Sub Main()
Dim n As Integer
n=6
Console.WriteLine("{0}!={1}",n,fact(n))
n=10
For i As Integer=1 To n
Console.Write("{0}{1}",fibo(i),IIF(n=i,vbCrLf,","))
Next
Console.Write("按任意鍵繼續(xù)。。。 ")
Console.ReadKey(True)
End Sub
' 遞歸算階乘
Function fact(n As Long) As Long
If 0=n OrElse 1=n Then Return 1
Return n*fact(n-1)
End Function
' 遞歸算斐波那契數(shù)列
Function fibo(n As Long) As Long
If 1=n OrElse 2=n Then Return 1
Return fibo(n-1)+fibo(n-2)
End Function
End Module
VB.net是面向?qū)ο笤O(shè)計(jì),所有的代碼都是儲(chǔ)存在類里面的,所以你寫代碼,前面都是public class,公用類
首先在代碼的開頭引入命名空間:
Imports?System.IO
給出三個(gè)讀文本文件的例子。
'直接讀取所有文本
Dim?fileString?As?String?=?File.ReadAllText("1.txt")
'把所有行弄進(jìn)一個(gè)數(shù)組
Dim?fileLines()?As?String?=?File.ReadAllLines("1.txt")
'以流的方式一行一行讀取
Using?fs?As?FileStream?=?File.OpenRead("1.txt")?'初始化文件流
Using?sr?As?New?StreamReader(fs)?'初始化流讀取器
Dim?lineString?As?String?=?""
While?Not?sr.EndOfStream?'循環(huán)直到流結(jié)束
lineString?=?sr.ReadLine()?'讀一行
'這里lineString就是每行的文件內(nèi)容了。
'你可以在這里對(duì)它進(jìn)行處理。
End?While
End?Using
End?Using
又碰你了……
好吧,這回代碼改進(jìn)了:
Code:
'我們需要幾個(gè)基本控件:
'TextBox1 、TextBox2、TextBox3 、TextBox4分別為“原文”、“輸入”、“準(zhǔn)確率”、“用時(shí)”的顯示文本框。
'Button1、Button2、Button3分別為“生成原文”、“開始輸入”、“結(jié)束”三個(gè)功能性按鈕。
'Timer1是計(jì)時(shí)器(Timer)組件。
'有幾個(gè)為了不出錯(cuò),需要把Enabled屬性設(shè)為False的組件:TextBox1、TextBox2、Button3。
'下面是整體代碼:(整個(gè)Form1.vb)
Public Class Form1
'聲明幾個(gè)需要的變量和隨機(jī)器。
Dim i As Random = New Random '隨即器
Dim TimeDate As Double = 0 '計(jì)時(shí)變量
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '生成原文按鈕。
Dim k As Integer
Dim pText As String = ""
For si As Int16 = 1 To 30 '隨即生成字符,添加到前面已聲明的pText變量內(nèi)。
k = i.Next(1, 26)
Select Case k '獲取對(duì)應(yīng)的字符。
Case 1
pText += "a"
Case 2
pText += "b"
Case 3
pText += "c"
Case 4
pText += "d"
Case 5
pText += "e"
Case 6
pText += "f"
Case 7
pText += "g"
Case 8
pText += "h"
Case 9
pText += "i"
Case 10
pText += "j"
Case 11
pText += "k"
Case 12
pText += "l"
Case 13
pText += "m"
Case 14
pText += "n"
Case 15
pText += "o"
Case 16
pText += "p"
Case 17
pText += "q"
Case 18
pText += "r"
Case 19
pText += "s"
Case 20
pText += "t"
Case 21
pText += "u"
Case 22
pText += "v"
Case 23
pText += "w"
Case 24
pText += "x"
Case 25
pText += "y"
Case 26
pText += "z"
End Select
Next
TextBox1.Text = pText
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
'處理文本內(nèi)容。
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click '開始輸入按鈕。
If TextBox1.Text = "" Then
MsgBox("還未生成原文!") '如果前面未生成原文時(shí)的警告。
Else
'如果原文已有,將執(zhí)行這些動(dòng)作(即開始輸入和準(zhǔn)備)
TextBox2.Enabled = True
TextBox2.Select()
Timer1.Enabled = True
Button3.Enabled = True
Button1.Enabled = False
Button2.Enabled = False
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick '計(jì)時(shí)器如果開始時(shí)的動(dòng)作。
TimeDate += 1 / 10
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click '結(jié)束按鈕。
Timer1.Enabled = False
Dim t As Double = TimeDate
TimeDate = 0
TextBox3.Text = t "秒"
Dim x As String, y As String
Dim i As Integer, s As Integer
s = 0
For i = 1 To Len(TextBox1.Text)
x = Mid(TextBox1.Text, i, 1)
y = Mid(TextBox2.Text, i, 1)
If x y Then
s = s + 1
End If
Next '尋找有幾個(gè)錯(cuò)誤。
'下面為計(jì)算正確率,使用Integer類型來約值。
Dim xi As Double = 1 - (s / 30)
Dim xiInt As Integer = xi * 100
TextBox4.Text = xiInt "%"
Button3.Enabled = False
Button1.Enabled = True
Button2.Enabled = True
End Sub
End Class
'到此結(jié)束,可以開始測試了。
'可以改進(jìn)改代碼后控件樣式,以更方便使用。
If?CheckBox1.Checked?And?CheckBox2.Checked?And?Not?CheckBox3.Checked?_
And?Not?CheckBox3.Checked?And?Not?CheckBox3.Checked?Then
MsgBox("滿足你的要求!")
Else
MsgBox("不滿足你的要求!")
End?If