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!

Recommended Answers

All 6 Replies

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

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

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.

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 :)

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++)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.