0

Okay so this is another question printing patterns but this time using input!
I have to prompt the user and read an odd number from the input. I can only use
System.out.print('*');
System.out.print(' ');
and System.out.println();

I need to maximize my use of repetition and minimize the number of output statements.
for examples:

n = 11
_____*
____***
___*****
__*******
_*********
***********
_*********
__*******
___*****
____***
_____*

n = 7
___*
__***
_*****
*******
_*****
__***
___*

where _ are spaces!

etc.

I haven't even begun this code as i have no idea how to start it!
Please help!

Edited by letters1417: n/a

3
Contributors
6
Replies
8
Views
5 Years
Discussion Span
Last Post by DavidKroukamp
0

this is no 'new issue'
this is exactly the same as your other thread.

if you need to input it in the command prompt, use an instance of the Scanner class.
if you need a dialogbox to enter the input, use JOptionPane

0

this is no 'new issue'
this is exactly the same as your other thread.

if you need to input it in the command prompt, use an instance of the Scanner class.
if you need a dialogbox to enter the input, use JOptionPane

and i would just make the index whatever the input is?

I'm sorry we haven't learned anything on patterns yet and this is homework that is trying to make us figure it out! So I have no background on printing any type of patterns

0

index? you're not using arrays, so there is not really and 'index'.
it's just plain logic:
if you want a square 4*4 to be printed
do 4 times[
do 4 times{
print *
}
]

will do the trick, for other shapes, you just need to know how to manipulate the values to use instead of the 4's up there.

0

this is no 'new issue'
this is exactly the same as your other thread.

if you need to input it in the command prompt, use an instance of the Scanner class.
if you need a dialogbox to enter the input, use JOptionPane

I have this:

for (int i = 1; i < n; i +=2) {
            for (int j = 0; j < n-1-i / 2; j++)
                System.out.print(" ");
            for (int j = 0; j < i; j++)
                System.out.print("*");
            System.out.print("\n");
        }
        for (int i = n-2; i > 0; i -= 2) {
            for (int j = 0; j < n - i/2; j++)
                System.out.print(" ");
            for (int j = 0; j < i; j++)
                System.out.print("*");
            System.out.print("\n");
        }

But it is missing the middle with the amount of stars input.... what am i missing?

EDIT
SOLVED :)

Edited by letters1417: n/a

0

I have this:

for (int i = 1; i < n; i +=2) {
            for (int j = 0; j < n-1-i / 2; j++)
                System.out.print(" ");
            for (int j = 0; j < i; j++)
                System.out.print("*");
            System.out.print("\n");
        }
        for (int i = n-2; i > 0; i -= 2) {
            for (int j = 0; j < n - i/2; j++)
                System.out.print(" ");
            for (int j = 0; j < i; j++)
                System.out.print("*");
            System.out.print("\n");
        }

But it is missing the middle with the amount of stars input.... what am i missing?

try:

for (int j = 0; j < n - i/2-1; j++)

Edited by DavidKroukamp: n/a

0

I have this:

for (int i = 1; i < n; i +=2) {
            for (int j = 0; j < n-1-i / 2; j++)
                System.out.print(" ");
            for (int j = 0; j < i; j++)
                System.out.print("*");
            System.out.print("\n");
        }
        for (int i = n-2; i > 0; i -= 2) {
            for (int j = 0; j < n - i/2; j++)
                System.out.print(" ");
            for (int j = 0; j < i; j++)
                System.out.print("*");
            System.out.print("\n");
        }

But it is missing the middle with the amount of stars input.... what am i missing?

try:

for (int j = 0; j < n - (i/2)-1; j++)

[EDIT] Great man sorry only saw it now

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.