vb练习题及答案

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

vb练习题及答案

VB练习题及答案

VB(Visual Basic)是一种广泛应用于Windows操作系统的编程语言,它以其简单易学的特点受到了许多初学者和专业开发者的青睐。为了帮助大家更好地掌握VB编程,下面将提供一些VB练习题及其答案,希望能对大家有所帮助。

练习题一:计算器程序

编写一个VB程序,实现一个简单的计算器功能。要求用户输入两个数字和一个操作符(+、-、*、/),然后根据操作符进行相应的计算,并输出结果。

解答:

```vb

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

Dim num1 As Double = CDbl(txtNum1.Text)

Dim num2 As Double = CDbl(txtNum2.Text)

Dim operator As String = cmbOperator.SelectedItem.ToString()

Dim result As Double

Select Case operator

Case "+"

result = num1 + num2

Case "-"

result = num1 - num2

Case "*"

result = num1 * num2

Case "/"

result = num1 / num2

End Select

lblResult.Text = "Result: " & result.ToString()

End Sub

```

练习题二:学生成绩管理系统

编写一个VB程序,实现一个简单的学生成绩管理系统。要求用户输入学生的姓名和成绩,然后将其保存到数组中。用户可以通过输入姓名来查询学生的成绩。

解答:

```vb

Private Structure Student

Public Name As String

Public Score As Double

End Structure

Private students() As Student

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

Dim newStudent As Student

= txtName.Text

newStudent.Score = CDbl(txtScore.Text)

ReDim Preserve students(students.Length)

students(students.Length - 1) = newStudent

txtName.Text = ""

txtScore.Text = ""

End Sub

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

Dim searchName As String = txtSearchName.Text

Dim found As Boolean = False

For Each student As Student In students

If = searchName Then

lblSearchResult.Text = "Score: " & student.Score.ToString()

found = True

Exit For

End If

Next

If Not found Then

lblSearchResult.Text = "Student not found."

End If

End Sub

```

练习题三:简单的文本编辑器

编写一个VB程序,实现一个简单的文本编辑器功能。用户可以打开一个文本文件,编辑其中的内容,并保存修改后的文件。

解答:

```vb

Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click

Dim openFileDialog As New OpenFileDialog()

openFileDialog.Filter = "Text Files (*.txt)|*.txt"

If openFileDialog.ShowDialog() = DialogResult.OK Then

Dim fileContent As String =

puter.FileSystem.ReadAllText(openFileDialog.FileName)

txtEditor.Text = fileContent

End If

End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

Dim saveFileDialog As New SaveFileDialog()

saveFileDialog.Filter = "Text Files (*.txt)|*.txt"

If saveFileDialog.ShowDialog() = DialogResult.OK Then

puter.FileSystem.WriteAllText(saveFileDialog.FileName, txtEditor.Text, False)

相关文档
最新文档