Originally posted by xiao sean:
hi guys,
i need a help on this assignment. the question is to require me to print a letter X onto screen using *.
but no matter how i try, the o/p is
**^^^
**^^^
***^^
**^*^
**^^*
any advice please?
-------------
/**
* Write a description of class ForEg3 here.
*
* @author (your name)
* @version (a version number or a date)
*/
class ForEg3
{
public static void main (String[]args)
{
for (int row=1; row <=5; row++)
{
for (int col=1; col <=5; col++)
if (row==col || (col <= (5-col)) )
System.out.print("*");
else System.out.print("^");
System.out.println();
}
}
}
---------
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Originally posted by xiao sean:
...i need a help on this assignment. the question is to require me to print a letter X onto screen using *...
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Originally posted by Fred Rosenberger:
[What i've done is put in a print statement that shows exactly what your if condition is each time through the loop. I put in a couple of printlns so that each row prints in a paragraph.
Try running this code, and look at the ouput. See if you can figure out what your problem is (although there may be more than one).[/QB]
Originally posted by sankar narayanan:
hi xiao sean,
I think, this solves your problem right?
Regards,
Sankar
[ August 22, 2007: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Originally posted by marc weber:
... It's your second condition (if col<=(5-col)) that's causing problems.
In your new code, you got closer by using a lot of "if" statements. But look at the numbers used in these statements. Do you see a pattern (a relationship between row and col) that could be expressed in a single line?
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Originally posted by marc weber:
In your original code, your first condition (if row==col) is correct. By itself, this produces...
It's your second condition (if col<=(5-col)) that's causing problems.
In your new code, you got closer by using a lot of "if" statements. But look at the numbers used in these statements. Do you see a pattern (a relationship between row and col) that could be expressed in a single line?
Originally posted by sankar narayanan:
[QB]hi xiao sean,
I think, this solves your problem right?
Regards,
Sankar
QB]
Originally posted by xiao sean:
...this really helps me alot. i'm able to execute the program.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Originally posted by xiao sean:
yes.i got what you mean. they actually form a 'X'. by crossing each other...
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
SCJP 6.0