Hi I'm new to Java, I'm supposed to create a program that reproduces a cross of stars where the user enters the number of stars to print.
Some how I'm supposed to be using it through nested loops.
Any suggestions will help. Thanks!
Printable View
Hi I'm new to Java, I'm supposed to create a program that reproduces a cross of stars where the user enters the number of stars to print.
Some how I'm supposed to be using it through nested loops.
Any suggestions will help. Thanks!
Have you tried writing anything yourself?
Give it a whirl on pencil-and-paper--try and visualize what you have to do and how you want to do it. Then put it into code best you can. Once you have a result (with or without errors), come to us with what you have and we'll help you through the process of getting it where you want it to end up.
i've been going at it for hours, lookin through the textbook.
what i've got so far is
import java.util.Scanner;
public class Cross1 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int number = 0, column = 0, row = 0;
System.out.println("Enter a number: ");
number = scan.nextInt();
for (row = number; row == column;)
System.out.print("*");
the hints i've been given are
1. When the row and column numbers are identical (i.e. when row = 0 and column = 0 there is a star, same for row = 1, and column = 1, etc.)
2. When the row number and the column number are opposite (symmetry): for example, for the number max of stars being 7, there is a star when row = 0 and column = 6, row = 1 and column = 5, row = 2 and column = 4, etc…
i just don't understand what i'm doing wrong. it supposed to be nested but i just can't get it
In first part, if the rows and columns are identical then the pattern is like this,
*****
*****
*****
*****
*****
isn't it?
nope, its supposed to look like this
if the user entered the number 7
*..........*
..*......*
....*.*
.....*
...*...*
..*.....*
*.........*
im not sure what cross posted means,
but i did google this question before hand and landed upon that same yahoo answer.
and found it easier to just copy and paste the way that person had showed the image.
the astericks are somehow supposed to draw the X sign.
i just dont understand how to follow the hints that were given inside my nested "for"
Cross-posting means that you've post the same question in multiple forums. Worst thing is that could discourage forums members to answering your question, including me. Because once someone found a solution from one forum, they never respect to others in other posted forums, and for there valuable time.
ohh i see.
i didn't cross post,
but with the exact same hints and question its a little hard to prove that i guess.
That's why Darryl ask that you've cross-posted it?
No it honestly was not me.
I just copied the way that person made the cross of the astericks to show what the end result was supposed to be.
Okay fine. So back to your question.
For the first hint, could you draw some pattern here to see, which has same number of rows and columns.
umm
like a square?
****
****
****
****
i have in my textbook an example of a nested loop where the astericks are a triangle
*
**
***
****
*****
******
i've been trying to use this example to help me solve the question
Fine, now you have any number of rows and same number of columns. (I'm talking about the first part only here). Think that you've start with the first row, in that row you've to deal with all the columns, it gives you something like,
*****
that is you've 5 columns. Then what you've to do is, move to the next row and do the same. Repeat this until end.
Code:for() { // Where the rows are switching
for() { // Where the columns are switching
// .....
}
}
Fine, now you have any number of rows and same number of columns. (I'm talking about the first part only here). Think that you've start with the first row, in that row you've to deal with all the columns, it gives you something like,
*****
that is you've 5 columns. Then what you've to do is, move to the next row and do the same. Repeat this until end.
Code:for() { // Where the rows are switching
for() { // Where the columns are switching
// .....
}
}
okay so i've got
where it prints out the number of stars that i want.Code:System.out.println("Enter a number: ");
number = scan.nextInt();
for (row = 1; row <= number; row++)
{
for (column = row; column <= row; column++)
System.out.print("*");
}
for (row = column; row<=number; row++)
{
for (column = row - number; column<=row; column++)
System.out.print(" ");
}
}
}
how can i make the same number of columns ??
what am i doing wrongg?
Please use code tags next time.