wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes help on printing letter X using * Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help on printing letter X using *" Watch "help on printing letter X using *" New topic
Author

help on printing letter X using *

xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
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();
}
}
}

---------
Joseph Kampf
Greenhorn

Joined: Mar 04, 2004
Posts: 26
[FBR - let's try to be a little nicer]


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();
}
}
}

---------

[ August 22, 2007: Message edited by: Fred Rosenberger ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

We're happy to help, but of course, nobody is going to give you the solution. Have you tried puting some extra println's in there to see what the variables are at each step?

for starters, i'd put on just before your 'if' condition, to see what row and col are each time through.

you will probably have to add something else though, to be sure you get BOTH the print and the if-else inside the body of the loop....


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 *...

Just to clarify, do you want the output to look something like this?

And do you want the '^' characters to show? Or are these supposed to be blank spaces?

(Joseph, you're right that we won't do people's homework for them. But xiao is only asking for "help on this assignment." And that's what we're here for. )
[ 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
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
thanks for replying. yes, you are right.

i am trying to get the exact output you execute just now...
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
i found a new solution. but it output more than what i expect

------
-------


output look like:

*^^^^*^
^*^^*^^
^^*^^^
^*^^*^^
*^^^*^

[edited to use code tags]
[ August 22, 2007: Message edited by: Fred Rosenberger ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

Please use code tags when posting your code. you can either highligh what you've pasted in, then click that little "code" button in the "instant UBB" section, or click the button first, then past your code between the XML-looking tags.

I think you have a problem with your logic. The first code sample you posted was close, but not quite there. Take a look at this code:


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).
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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?
sankar narayanan
Greenhorn

Joined: Aug 02, 2007
Posts: 11
hi xiao sean,



I think, this solves your problem right?

Regards,
Sankar
[ August 22, 2007: Message edited by: marc weber ]
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
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]




thanks for the reply, after i look through and compare the codes with 'X', actually all these codes doesnt match. for eg. if (5==1) able to fufill * but (1<= (5-1)) actually cant fufill the * if we place it into the 'X'.

if i think at this way is wrong, please enlighten me.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

Sankar,

As Fred said above, "We're happy to help, but of course, nobody is going to give you the solution." Or as it says at the top of the beginner's forum, "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers." Especially when this is homework.

Thanks!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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?

Hint: What's always true about row and col if...
(row == 1 && col == 5) or
(row == 2 && col == 4) or
(row == 4 && col == 2) or
(row == 5 && col == 1) ?
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
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?




hi, thanks for your reply and help. this really helps me alot. i'm able to execute the program.
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
Originally posted by sankar narayanan:
[QB]hi xiao sean,


I think, this solves your problem right?

Regards,
Sankar

QB]



Hi Sankar, thanks for your great help. but i prefer to debug on my own. really thanks alot.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by xiao sean:
...this really helps me alot. i'm able to execute the program.

Yeah, that's a cleaned up version of what you had earlier. But there's a much shorter way to say...

(row == 1 && col == 5) || (row == 2 && col == 4) || ( row == 4 && col == 2) || (row == 5 && col == 1)

In each of these cases, what's the relationship between row and col?
sankar narayanan
Greenhorn

Joined: Aug 02, 2007
Posts: 11
hello Ranchers,

Ok. I found the mistake I have done. I am also here to learn.

Thanks for notifying.

Regards,
Sankar
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

when programming, you often have to look for patterns. here, try and find a relationship between

1 and 5
2 and 4
3 and 3
4 and 2
5 and 1

what do all these pairs of numbers have in common? you could say "well, they're all less than 6. but can you see any ADDITIONal ways they are related?
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
yes.i got what you mean. they actually form a 'X'. by crossing each other....

thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by xiao sean:
yes.i got what you mean. they actually form a 'X'. by crossing each other...

Yes, but... As Fred said, there's something additional to notice.
prakash chandra
Greenhorn

Joined: Aug 14, 2007
Posts: 24
Here is the simple general solution. Just modify the value of SIZE variable in the code to print different size of X with *. But the value must be an odd number.


SCJP 6.0
 
GeeCON Prague 2014
 
subject: help on printing letter X using *