Really easy question, but very frustrating. Newbie alert
Hello people, first time poster.
I have been fooling with vb for a couple of weeks now and have a question.
I need help coding, and i know its probably right in front of me. If i were to design a program that would let me enter the size of a diamond and then drawn a diamond of that size using only odd numbers
like the diamond size is 9 and the drawing would look like
*
* * *
* * * * *
* * * * * * *
* * * * * * * * * - this is where the 9 input would go
* * * * * * *
* * * * *
* * *
*
this is just a little fun project on the side, but i wanted to see if i could do it
the output can be in console, message box, or list box
I know how do declare variables and to check for valid entries
in advance thanks for you help.
Simon
Re: Really easy question, but very frustrating. Newbie alert
This is how I would do it:
VB Code:
Public Shared Function CreateTextDiamond(ByVal MaximumValue As Integer) As String
Dim Diamond As String
'Build Increasing Half
For I As Integer = 1 To MaximumValue Step 2
'Append(add) a string made of '*' I long and add a new line to Diamond
Diamond &= New String("*"c, I) & ControlChars.CrLf
Next
'Build Decreasing Half
'We Need to subtract 2 from MaxiumValue otherwise there wil be two lines MaximumValue Long
For D As Integer = (MaximumValue - 2) To 1 Step -2
'Append(add) a string made of '*' I long and add a new line (Crlf)
Diamond &= New String("*"c, D) & ControlChars.CrLf
Next
'Done return the generated Diamond
Return Diamond
End Function
Usage:
VB Code:
Msgbox (CreateTextDiamond(9))
Re: Really easy question, but very frustrating. Newbie alert
Thanks for your help, but i think thats is a more complicated than i was thinking. On my design i have a textbox for input, a calculate button so when you press it then i either need a message box or a list box shows that shows the diamond. I was reading in abook about loops, can this be done using loops starting with the "Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click" code
Thanks again for your help
sorry if this is lame
Re: Really easy question, but very frustrating. Newbie alert
Quote:
Originally Posted by rasdak24
Thanks for your help, but i think thats is a more complicated than i was thinking. On my design i have a textbox for input, a calculate button so when you press it then i either need a message box or a list box shows that shows the diamond. I was reading in abook about loops, can this be done using loops starting with the "Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click" code
Thanks again for your help
sorry if this is lame
He is using a loop. 2 of them. They are For Next loops that are counting by twos.
Re: Really easy question, but very frustrating. Newbie alert
thanks for all your help people, I am still not getting what i am looking for, but i do thank you for your help. can this be put into a do while loop using a button click thanks
Re: Really easy question, but very frustrating. Newbie alert
Here is a complete copy-and-paste sample
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Public Shared Function CreateTextDiamond(ByVal MaximumValue As Integer) As String
Dim Diamond As String
'Build Increasing Half
For I As Integer = 1 To MaximumValue Step 2
'Append(add) a string made of '*' I long and add a new line to Diamond
Diamond &= New String("*"c, I) & ControlChars.CrLf
Next
'Build Decreasing Half
'We Need to subtract 2 from MaxiumValue otherwise there wil be two lines MaximumValue Long
For D As Integer = (MaximumValue - 2) To 1 Step -2
'Append(add) a string made of '*' I long and add a new line
Diamond &= New String("*"c, D) & ControlChars.CrLf 'Diamond
Next
'Done return the generated Diamond
Return Diamond
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sValue As String = InputBox("What Number?")
If IsNumeric(sValue) Then
Dim iValue As Integer = Integer.Parse(sValue)
MsgBox(CreateTextDiamond(iValue))
Else
MsgBox("you did not enter a valued numeric expression!")
End If
End Sub
Re: Really easy question, but very frustrating. Newbie alert
Thanks thats exactly what i needed.
Thank you very much
simon