Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MBP123

macrumors regular
Original poster
May 26, 2006
192
0
To print out this output

********
* *
* *
* *
* *

right now i have the top row and the left aligned stars, but cant get the right aligned ones.

Code:
int HorizLine, VertLine;
        
        for (HorizLine=1; HorizLine<=1; HorizLine++)
	   {
		for (VertLine=1; VertLine<=10; VertLine++)
				{
				System.out.print("*");
				}
			System.out.print("");
			}//row of solid asteriks
        
        for (HorizLine=1; HorizLine<=4; HorizLine++)
           {
                for (VertLine=1; VertLine<=1; VertLine++)
                                {
                                System.out.print("\n");
                         System.out.print ("*");//left aligned asteriks

could someone please steer me in the right direction as to how to get them to be in line with the other side? thanks
 

MBP123

macrumors regular
Original poster
May 26, 2006
192
0
well the image didnt display properly in the forum....but its supposed to the one row of stars in line with the first asterisk, and the other line of asterisks in line with the last asterisk of the top row.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Well, if you can assume fixed width fonts (seems the only way doing what you are doing) I would simply print one "*" then 8 " " then another "*".
 

MBP123

macrumors regular
Original poster
May 26, 2006
192
0
no its not fixed because the user enters the width by inputting a digit earlier in the program. so it can be anything between 5 and 20.

so its more like

Code:
for (HorizLine=1; HorizLine<=1; HorizLine++)
	   {
		for (VertLine=1; VertLine<=width; VertLine++)
				{
				System.out.print("*");
				}
			System.out.print("");
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
OK. If I assume your width is stored in an int called width:

Code:
int width;

<get user to input and validate storing in width>

int i,j;

for (i=0;i<width;i++)
{
System.out.print("*");
}
System.out.print ("\n");

for (j=0;j<4;j+)
{
System.out.print("*");
for (i=0;i<(width-2);i++)
{
System.out.print(" ");
}
System.out.print("*\n");
}
 

Gelfin

macrumors 68020
Sep 18, 2001
2,165
5
Denver, CO
I'm assuming this is a homework assignment, so I'm not going to provide an answer, but here are a couple of hints:

First, learn how to indent correctly. If your instructor isn't knocking at least five points off for that, he should.

Second, if you recall last time you posted a question like this, I replied with an unconscionably smartassed solution that did the bulk of the work on one line. That solution was terrible because it was unreadable and unmaintainable, but the general approach was valid, and since this is a much simpler problem, you'd do well to consider it:

You are writing out a square of characters with known height and width, one coordinate at a time. Treat that as a separate problem. Do your nested loops, and inside them call a method whose job it is to figure out whether or not a star should be written at a given X and Y coordinate. Think about the conditions that make a position a "star" position instead of a "space" position. If your method returns true, write a star. If it returns false, write a space.

In your current problem, the conditions are very simple -- far more so than in that checkerboard+diamond problem. A position should contain a star if it is in the top row, the leftmost column, or the rightmost column (assuming I've understood your assignment correctly). You can easily define a test method for those conditions in a few lines of code.

If you want to really impress your instructor, make a general class that writes out a matrix of stars (define your test method to always return true) then each time he assigns you one of these problems reuse the general class by deriving a subclass that overrides your test method with one that returns the correct values for the assignment.

I mean, don't be surprised if he thereafter gives you an assignment that breaks your base class by asking for a matrix with more than two kinds of character in it. Compensating for that possibility upfront is left as an exercise you may easily be forgiven for not wanting to undertake just now.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.