实验三第1题四则运算题设计如下窗体,为五个命令按钮编写5段代码完成四则运算及退出程序,其中在计算除法时,如果第二个数就是零,在消息框中显示信息“除数为零”。
Public Class Form1Private Sub Button1_Click(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles Button1、ClickDim a, b As Singlea = TextBox1、Textb = TextBox2、TextTextBox3、Text = a + bEnd SubPrivate Sub Button2_Click(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles Button2、ClickDim a, b As Singlea = TextBox1、Textb = TextBox2、TextTextBox3、Text = a - bEnd SubPrivate Sub Button3_Click(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles Button3、ClickDim a, b As Singlea = TextBox1、Textb = TextBox2、TextTextBox3、Text = a * bEnd SubPrivate Sub Button4_Click(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles Button4、ClickDim a, b As Singlea = TextBox1、Textb = TextBox2、TextIf b = 0 ThenMessageBox、Show("除数为零")ElseTextBox3、Text = a / bEnd IfEnd SubPrivate Sub Button5_Click(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles Button5、ClickEndEnd SubEnd Class第2题一元二次方程求解输入系数后,单击“求解”按钮后,根据判别式的3种不同情况,分别显示如下的不同结果。
Public Class Form1Private Sub Button1_Click(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles Button1、ClickDim a, b, c, delta As Doublea = TextBox1、Textb = TextBox2、Textc = TextBox3、Textdelta = b * b - 4 * a * cIf delta > 0 ThenLabel5、Visible = TrueLabel6、Visible = TrueLabel4、Text = "方程有两个不相等实根"Label5、Text = "X1=" & ((-b + Math、Sqrt(delta)) / 2 / a)Label6、Text = "X2=" & ((-b - Math、Sqrt(delta)) / 2 / a)ElseIf delta = 0 ThenLabel4、Text = "方程有两个相等实根"Label5、Visible = TrueLabel5、Text = "X1=X2=" & (-b / 2 / a)Label6、Visible = FalseElseLabel4、Text = "方程没有实根"Label5、Visible = FalseLabel6、Visible = FalseEnd IfEnd SubPrivate Sub Button2_Click(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles Button2、ClickEndEnd SubEnd Class第3题文本框中前景色与背景色的不同组合目的:单选按钮的使用(1)设计窗体如下所示窗体中包含两组单选按钮、两个框架与一个命令按钮。
(2)程序功能:分别单击不同的前景色与背景色按钮,在文本框中显示颜色的改变。
(3)设置控件的主要属性1)窗体的文本属性为“不同前景色背景色的组合”2)初始前景色为红色、背景色为黄色3)其它属性根据图中显示来设置(4)事件代码Public Class Form1Private Sub RadioButton1_CheckedChanged(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles RadioButton1、CheckedChangedTextBox1、ForeColor = Color、RedEnd SubPrivate Sub RadioButton2_CheckedChanged(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles RadioButton2、CheckedChangedTextBox1、ForeColor = Color、GreenEnd SubPrivate Sub RadioButton3_CheckedChanged(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles RadioButton3、CheckedChangedTextBox1、ForeColor = Color、BlackEnd SubPrivate Sub RadioButton4_CheckedChanged(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles RadioButton4、CheckedChangedTextBox1、BackColor = Color、WhiteEnd SubPrivate Sub RadioButton5_CheckedChanged(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles RadioButton5、CheckedChangedTextBox1、BackColor = Color、YellowEnd SubPrivate Sub Form1_Load(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles MyBase、LoadEnd SubEnd Class第4题三位整数的反序显示在窗体中添加二个文本框、二个标签与一个命令按钮,在一个文本框中输入一个三位的整数,单击命令按钮后,在另一个框中按相反顺序显示该数,例如,输入123,则在另一个文本框中显示321。
提示:本题先将每位上的数字分解出来,这要用到整除与计算余数的运算符,例如123整除100后的结果1就就是百位,而123除以10的余数就就是个位,至于十位,大家可以自己确定。
分解后的三位数再倒序组合成新的三位整数。
Public Class Form1Private Sub Button1_Click(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles Button1、ClickDim a, b, c, d, y As Singlea = TextBox1、Textb = a \ 100c = (a - 100 * b) \ 10d = a - 100 * b - 10 * cTextBox2、Text = d & c & bEnd SubEnd Class试验五第1题累加编写程序,计算1~100之间的所有偶数之与,程序窗口界面及控件自行设计。
Public Class Form1Private Sub Button1_Click(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles Button1、ClickDim i, sum As LongFor i = 2 To 100 Step 2sum += iNextTextBox1、Text = sumEnd SubEnd Class第2题使用循环求解水仙花数的问题要求:(1)参考教材上例4-3关于水仙花数的定义。
(2)程序窗口界面及控件与例4-3的结果即图4-3一样。
(3)单击“开始”按钮后,在文本框中显示求出的结果。
Public Class Form1Private Sub Button1_Click(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles Button1、ClickDim M, k1, k2, k3 As IntegerTextBox1、Clear()For M = 100 To 999k1 = M \ 100k2 = (M \ 10) Mod 10k3 = M Mod 10If M = k1 ^ 3 + k2 ^ 3 + k3 ^ 3 ThenTextBox1、Text += M、ToString() + " = " + k1、ToString() + "^3+" + k2、ToString() + "^3+" + k3、ToString() + "^3" + vbCrLfEnd IfNextEnd SubEnd Class第3题统计整数的因子编写程序,向一个文本框输入一个整数,单击“开始”按钮后,找出该数的所有因子,并统计因子的个数与总与,结果在另一个文本框中显示,共有三行,其中第一行该数的所有因子,第二行显示该数的因子个数,最后一行显示该数所有因子之与,窗体界面如下:Public Class Form1Private Sub Button1_Click(ByVal sender As System、Object, ByVal e As System、EventArgs) Handles Button1、ClickDim a, b, c, d As Longc = 0a = TextBox1、Textb = 1While b <= aIf a Mod b = 0 Thenc += 1d += bTextBox2、Text = "该整数的因子就是:" & +=CStr(b) + "," + vbCrLf + "因子个数为:" + c、ToString() + vbCrLf + "因子之与为:" + d、ToString()End Ifb = b + 1End WhileEnd SubEnd Class第4题判断素数编写程序,判断某个整数就是否就是素数,在一个文本框中输入一个整数,单击命令按钮后,在另一个文本框中显示该数就是否就是素数,程序代码可以参考例4-10的代码,在此基础上改写。