| Date | 2011/4/10 |
|---|---|
| Name | BullsAndCows |
| Description | A number guessing game written in VB.Net |
| Filename | BullsAndCowsVB.vb |
| Content |
|
| Sample Input: | |
| Sample Output: | |
| Expected Test Command: | vbc BullsAndCowsVB.vb BullsAndCowsVB.exe |
Module BullsAndCows
Sub Main
Dim secretDigit(4) As Integer,arr(4) as Integer
Dim i As Integer, j As Integer
Dim bull As Integer,cow As Integer
Dim msg As String, secret As String
Dim playNextGame As String,ans As String
Randomize
Call System.Console.WriteLine("Bulls and Cows")
Call System.Console.WriteLine("==============")
Call System.Console.WriteLine("This is an ancient game with guessing numbers.")
Do
secret=""
playNextGame=""
For i=0 To 3
secretDigit(i)=CInt(RND(1)*10)
secret=secret & secretDigit(i)
Next i
Call System.Console.WriteLine("A secret number of 4-digit has been draw!")
Dim regex As new System.Text.RegularExpressions.RegEx("^\d{4}$|^quit$")
Do
Do
Call System.Console.Write("Please enter a 4-digit number (or type 'quit' to exit):")
ans=Trim("" & System.Console.ReadLine())
Loop while Not regex.IsMatch(ans)
If ans="quit" Then
Call System.Console.WriteLine("The secret number is: " & secret)
Else
cow=0
bull=0
For i=0 To 3
arr(i)=CINT(Mid(ans,i+1,1))
If arr(i)=secretDigit(i) Then bull=bull+1
Next i
For i=0 To 3
For j=0 To 3
If arr(j)=secretDigit(i) Then
cow=cow+1
arr(j)=-1
j=3
End If
Next
Next
cow=cow - bull
msg=""
If cow=0 And bull=0 Then
msg="Neither a bull nor a cow!"
Else
If bull=4 Then
msg="4 bulls! You are a genius!"
Else
If cow=0 Then
If bull=1 Then
msg="1 bull only."
Else
msg=bull & " bulls only."
End If
ElseIf bull=0 Then
If cow=1 Then
msg="1 cow only."
Else
msg=cow & " cows only."
End If
Else
If bull=1 Then
msg="1 bull and "
Else
msg=bull & " bulls and "
End If
If cow=1 Then
msg=msg & "1 cow."
Else
msg=msg & cow & " cows."
End If
End If
End If
End If
Call System.Console.WriteLine(msg)
End If
Loop while bull<>4 And ans<>"quit"
If ans<>"quit" Then
Do
Call System.Console.Write("Do you want to play a new game (Y/N)?")
playNextGame=System.Console.ReadLine()
Loop while playNextGame<>"Y" And playNextGame<>"N" And playNextGame<>"y" And playNextGame<>"n"
End If
Loop while (playNextGame="Y" Or playNextGame="y") And ans<>"quit"
Call System.Console.WriteLine("Thank you for playing Bulls and Cows!")
End Sub
End Module| Date | 2011/4/9 |
|---|---|
| Name | Add Two Numbers |
| Description | Add Two Numbers using VB.Net |
| Filename | AddTwoNumVB.vb |
| Content |
|
| Sample Input: | 123 3.5 dsfasd 4 fdsafds 6 |
| Sample Output: | 126 0 |
| Expected Test Command: | vbc AddTwoNumVB.vb AddTwoNumVB.exe < testdata.txt |
Module AddTwoNumVB Sub Main Dim total,nCount As Integer,num As Integer Dim temp as String Dim arr as String() Do temp=System.Console.ReadLine If Not temp is Nothing Then arr=System.Text.RegularExpressions.Regex.split(temp,"\s+") For Each value As String In arr If System.Int32.TryParse(Value,num) Then If nCount mod 2=0 Then total=num Else total=total+num System.Console.Write(total & " ") End If nCount = nCount + 1 End If Next End If Loop While Not temp is Nothing End Sub End Module
| Date | 2011/4/8 |
|---|---|
| Name | Hello Friend |
| Description | Ask user's name and echo greeting message to user using VB.Net |
| Filename | HelloFriendVB.vb |
| Content |
|
| Sample Input: | Peter Pan |
| Sample Output: | Hello, Peter Pan! |
| Expected Test Command: | vbc HelloFriendVB.vb HelloFriendVb.exe < testdata.txt |
Module HelloWorldFriendVB
Sub main()
Dim name As String
Do
System.Console.Write("Your name?")
name=System.Console.ReadLine
If Not name is Nothing Then
System.Console.WriteLine("Hello, " & name & "!")
End If
Loop While Not name is Nothing
End Sub
End Module
| Date | 2011/4/7 |
|---|---|
| Name | Hello World |
| Description | Display greeting message to user using VB.Net |
| Filename | HelloWorldVB.vb |
| Content |
|
| Sample Input: | |
| Sample Output: | Hello, World! |
| Expected Test Command: | vbc HelloWorldVB.vb HelloWorldVB.exe |
Module HelloWorld
Sub Main()
System.Console.WriteLine("Hello, World!")
End Sub
End Module