Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1. Write a program which allows the user to enter their age. The program should then print either "You are
still young" or "You are getting old" depending on whether the person's age is greater than 100 or not.
2. Write a program in which the user can enter two numbers (integers). If both numbers are zero
then print the message "Two zeros". If the numbers are not both zero, then print either "FIRST" or
"SECOND" depending on which number is bigger. If the numbers are the same (but not both zero)
then print "EQUAL"
3. Write a program which allows the user to enter a number of pollution readings (integers). As
each pollution reading is entered the program should test the reading and print one of the following
messages:
Comfortable (for readings less than 30)
Moderate (for readings less from 30 to 60 inclusive)
Dangerous (for readings over 60)
4. Write a program in which the user enters their salary. The program is to calculate and display
the amount of tax payable on this salary. Use the (simple) tax rule: Salaries under $4000 pay 2%
tax, salaries of $4000 or more pay 65% tax (!)
5 10 15 ... etc
1 3 6 10 15 21 28 36
functions
private sub showmsg(byval pet as string, byval age as integer)
age+=1 label1.text = "your pet, " & pet & "will be " & age & "years old"
end sub
age = inputbox("age")
integer.tryparse(inputage,currentage)
private sub grosspay(byval hours as double, byval rate as double, byref gross as double)
end if
'button click
double.tryparse(textbox1.text,hourswkd)
double.tryparse(textbox2.text,rateofpay)
grosspay(hourswkd,rateofpay,grosspay)
lblGross.text = grosspay.tostring("C2")
return area
end function
'button click
double.tryparse(textbox1.text , circleradius)
circlearea = getcirclearea(circleradius)
label1.text = circlearea.tostring("N2")
userhous = val(txthours.text)
wage = val(txtwage.text)
displaypay(userhours, wage)
else
earnings = HOUR_LIMIT * rate
end if
end sub
return true
end if
end function
Function BoxVolume( Optional ByVal length As Integer = 1,Optional ByVal width As Integer = 1,
Optional ByVal height As Integer = 1 ) As Integer
End Function
'button click
label1.text = BoxVolume()
label2.text =BoxVolume(10)
'clock timer
'square root
End Sub
manipulating strings
'determining number of characters in a string
Dim strname As String
Dim intnumchars As Integer
strname = TextBox1.Text
intnumchars = strname.Length
MsgBox(intnumchars)
Do
strname = InputBox("enter 5 letter code", "code")
Loop Until strname.length = 5
strname = TextBox1.Text.Trim
strname = strname.Trim
Label1.Text = strname
'string,remove
strname = TextBox1.Text
'strname = strname.Remove(0, 4)
strname = strname.Remove(4)
Label1.Text = strname
If strid.Length = 5 Then
ListBox1.Items.Add(strid.ToUpper)
Else
MessageBox.Show("the id must contain 5 characters", "id", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
'padding strings
strname = TextBox1.Text
'strname = strname.PadLeft(5)
'strname = strname.PadRight(10)
strname = strname.PadLeft(5, "*")
Label1.Text = strname
'searching a string
strname = TextBox1.Text
If strname.ToUpper.Contains("ELM ST.") Then
MsgBox("string found")
End If
Label1.Text = strname.Contains("In")
intname = strname.IndexOf("ID")
Label1.Text = intname
strname = TextBox1.Text
strfirst = strname.Substring(0, 4)
strlast = strname.Substring(5)
Label1.Text = strfirst
'Label1.Text = strlast
'rearrange name
Else
MessageBox.show("invalid name format", "rearrange name", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End If
LIKE operator allows you to use pattern matching characters to determine whether one string is
equal to another string
strname = TextBox1.Text
If strname Like "#####?" Then
MessageBox.Show("string matches")
Else
MessageBox.Show("string does not match")
End If
'check id format
strname = TextBox1.Text
TextBox1.Text = strname.Replace("old", "new")
strname = TextBox1.Text
'replace string
TextBox1.Text = strname
strname = TextBox1.Text
Mid(strname, 1) = "ok"
TextBox1.Text = strname