I have to display the following pattern using a loop:

@@@@@@@@@&
@@@@@@@@&&
@@@@@@@&&&
@@@@@@&&&&
@@@@@&&&&&
@@@@&&&&&&
@@@&&&&&&&
@@&&&&&&&&
@&&&&&&&&&

I have the following program that is using a loop but it displays 9 rows of 10 @ symbols instead of incorporating the & symbols.

What do I need to do to make it work to display like the picture above?

public class Symbols
{
	private int width;
	
	public Symbols(int aWidth)
	{
		width = aWidth;
	}
	
	public String toString()
	{
		String r = "";
		for (int i = 1; i<= width; i++)
		{
			for(int j = 1; j<= width; j++)
				r = r +"@";
			r = r + "\n";
		}
		return r;
	}
}
public class SymbolsTester
{
	public static void main(String [] args)
	{
		Symbols symbols = new Symbols(9);
		System.out.println(symbols.toString());
	}

}

What do I need to do to make it work to display like the picture above?

Look at what your code does for each statement it executes. Play computer by tracking thru the logic using a piece of paper and a pencil writing down the values of the variables as each statement is executed.

Where are the '&' characters in your code? It will never print the '&' without some code to do that.

Edited 5 Years Ago by NormR1: n/a

You only need one loop.
In that loop, decide (if) which character you will print.

I like the fact that you are building and returning the string for printing.

You only need one loop.
In that loop, decide (if) which character you will print.

I like the fact that you are building and returning the string for printing.

you use a loop per line, so you can check by character where to change to the second one,
but you'll need a loop to print the different lines.

I have to admit, I would not immediately know how to code this entirely with one loop.

Some thoughts without having written the code:
The one loop would be over the total rows*columns characters in the output.
The if tests would be for what character to output at the current index value.

So if I leave it with two loops...where do I need to insert code to have it start using the & symbol and any hints on what that code should be?

Look at each row and position
For the first row How many @ and how many & are on the row
then for the second row: it has x @s and y &s
How do the values of x and y change depending on the row.
You can make an equation for x and y that is a function of the row number.

just a bit pseudocode:

set maxLength
set char1 to &
set char2 to @

set num = 1

do ( maxLength times ){

empty emptyString

do ( maxLength times : counter ){

if ( counter < maxLength - num )
  emptyString = emptyString + char1
else
  emptyString = emptyString + char2
end-if


}

print emptyString
add 1 to num

}

This is generated by one loop:
$$$$$$$$$@
$$$$$$$$@@
$$$$$$$@@@
$$$$$$@@@@
$$$$$@@@@@
$$$$@@@@@@
$$$@@@@@@@
$$@@@@@@@@
$@@@@@@@@@

and this is without a hardcoded number of if's (one for each char) or a switch and with a dynamical size?

must 've missed something (and I mean that I must've missed something, by saying that :) )

Edited 4 Years Ago by stultuske: to avoid confusion

This article has been dead for over six months. Start a new discussion instead.