![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 4
| here is what I have, just don't understand how to make it hollow, do I use a loop or if else.. Code:
static void Main(string[] args)
{
int side;
Console.Write("Enter the side of your square: ");
side = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
for (int i = 0; i < side;++ i )
{
for (int j = 0; j < side; ++j )
Console.Write("* ");
Console.WriteLine();
}
Console.WriteLine();
}
}
Last edited by mdnealy; 07-04-2009 at 06:14 PM. |
| mdnealy is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Either (a) you'll need to decide, inside your loop whether to print a * or a space--if you look at coordinates (your i and j), what's true when you need to print * and what's true when you need to print space or (b) you'll have to adjust your loops slightly to print "side" vs "inside". |
| tabstop is offline | |
| | #3 |
| Super Moderator Join Date: Aug 2001
Posts: 7,470
| Why in the world are you writing asterisks in C# when you have so many simple graphics at your disposal? If this is for an assignment, I understand, but IMO they sorely need to alter the curriculum a bit. Simple window-graphics are about as easy and basic as they can be in C# .NET.
__________________ If you aim at everything you will hit something but you won't know what it is. |
| Bubba is offline | |
| | #4 |
| Registered User Join Date: Mar 2009 Location: england
Posts: 76
| Seems like a weird way to draw a rectangle, but anyway... Code: using System;
namespace ConsoleApplication4
{
class Program
{
static void Main()
{
Console.Write("select the length of the sides: ");
int len;
if (!int.TryParse(Console.ReadLine(), out len))
Main(); // invalid input
if (len < 1 || len > 20)
Main(); // invalid input
for (int i = 0; i < len; i++)
{
if (i == (len - 1) || i == 0) // first or last line
Console.WriteLine(Repeater("* ", len));
else // middle line
Console.WriteLine("* " + Repeater(" ", (len - 2)) + "* ");
}
Console.WriteLine();
Main();
}
static String Repeater(String text, int count)
{
String tmp = "";
for (int i = 0; i < count; i++)
tmp += text;
return tmp;
}
}
}
|
| theoobe is offline | |
| | #5 |
| Registered User Join Date: Jul 2009
Posts: 4
| I am just starting out in C# so please be a little patient .When I run the program I posted, it will give me a full square, i don't want a rectangle. but what i am having trouble with is how I make it hollow. The book we are learning from is easy when I put a program together like a gradebook or a calculator, but to create a graphic picture in console is throwing me off. The square can only have an input from 1 to 20. I do not have any examples in which to complete this in. I can create gui's but this is console only. In the post above this, it has commands that we have not gotten to. I am a quick study but I learn with repition(sp). It is an assignment but I am just having trouble grasping it. Once I see the code, I can usually build off of that. Dietel books are a little vague and I did not go over this in class... Last edited by mdnealy; 07-05-2009 at 07:23 AM. |
| mdnealy is offline | |
| | #6 |
| Registered User Join Date: Mar 2009 Location: england
Posts: 76
| I used the word Rectangle because in Graphics, a square would fall into the designation of Rectangle, although obviously all the sides will be the same length. Could you please explain how the code I've posted fails to meet your criteria? |
| theoobe is offline | |
| | #7 |
| Registered User Join Date: Jul 2009
Posts: 4
| Code: if (!int.TryParse(Console.ReadLine(), out len))
Main(); // invalid input
if (len < 1 || len > 20)
Main(); // invalid input
for (int i = 0; i < len; i++)
{
if (i == (len - 1) || i == 0) // first or last line
Console.WriteLine(Repeater("* ", len));
else // middle line
Console.WriteLine("* " + Repeater(" ", (len - 2)) + "* ");
}
Console.WriteLine();
Main();
}
static String Repeater(String text, int count)
{
String tmp = "";
for (int i = 0; i < count; i++)
tmp += text;
return tmp;
![]() I am trying to rewrite it to match up to what we have learned so far. |
| mdnealy is offline | |
| | #8 |
| Registered User Join Date: Mar 2009 Location: england
Posts: 76
| Ok, well int.TryParse works by evaluating a string and converting the result to an integer, similar to Convert.ToInt32(). However it is useful in that it incorporates error handling. In other words, if the user entered something non-numeric, rather than C# throwing an exception, it will simply return FALSE instead. Example 1: Code: int i;
bool test = int.TryParse("12345", out i);
Example 2: Code: int i;
bool test = int.TryParse("hello world", out i);
Hope this helps. :-) |
| theoobe is offline | |
| | #9 |
| Registered User Join Date: Jul 2009
Posts: 4
| it helped tons and made more sense. thanks for taking the time to do this. this discussion board has given me more answers than i have found in about 4 days!! thanks again for your help!! |
| mdnealy is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Loop seg error | Zishaan | Game Programming | 2 | 03-28-2007 01:27 PM |
| Forced moves trouble!! | Zishaan | Game Programming | 0 | 03-27-2007 06:57 PM |
| Help with my draughts game | Zishaan | C++ Programming | 9 | 03-24-2007 07:33 AM |
| hollow square | jms318 | C Programming | 5 | 11-30-2006 11:36 PM |
| Hollow square | Kayoss | C++ Programming | 4 | 09-16-2005 04:49 PM |