vbscript code for pattern design
can anyone help me with a vb code that i'm struggling to do. I'm just a novice when it comes to programming.
Exercise says: Get an integer n from a user, print out the following pattern on the screen depending o开发者_如何学编程n user input Pattern 1 - when n is 3, there are 5 rows in total pattern 2 - when n is 4, there are 7 rows in total
In the output pattern 1 should look like this
*
***
*****
***
*
Divide and conquer is the way to solve these problems, as with most computing problems.
So split your task up into sub problems. I can see three sub problems:
Calculating number of rows
Create a function that takes n as an input, and returns the total number of rows. I'll leave it up to you to decide the logic for this, perhaps you can scale it so it works for a wider range of numbers? Perhaps there is a hidden formula or particular piece of logic behind the return value? Or perhps you just need a select case statement.
Working out the biggest line width
Think about how to logically solve this problem, what do you need to learn about the pattern to continue after this step? I think you need to work out what the width of the middle row is. That would be an excellent starting point. We know the biggest line width from calling our previous function, and this is all the information we need to work out the biggest line width.
Printing the resulting picture
You now know the number of rows in this picture based on the function you wrote earlier, and the biggest size of the middle row. You should be able to loop through printing the characters correctly now, but you will have to work out how to fill the blank spaces to align characters properly.
Input sets the midpoint (peak) of your diamond
' Building up to the peak
for (i = 1, i <= input, i++)
{
dots = 1 + (2 * (i-1))
peakdots = 1 + (2 * (input - 1))
spaces = (peakdots - dots) / 2
wscript.echo spaces & dots & spaces
}
' On the way back down
for (i = (input - 1), i > 1, i--)
{
dots = 1 + (2 * (i-1))
peakdots = 1 + (2 * (input - 1))
spaces = (peakdots - dots) / 2
wscript.echo spaces & dots & spaces
}
The for loop needs to be tweaked from C-style, but the contents should work as-is.
精彩评论