hello I need to create a method called displayPattern, example in main method us
ID: 3633989 • Letter: H
Question
hello I need to create a method called displayPattern, example in main method user gives 6 or 12 or 22 (height) and also provides the character, with thise I have my method code like this (check below). but I need it to make it exact height as they gave me (6, 12, or 22), somehow my code only gives me 5, 11, or 21. can you please correct my loop inside my method so it works.
Here example as wanted (height 6)
*
***
*****
*****
***
*
This below is what i get with current code (dont need this)
*
***
*****
***
*
CODE: METHOD
public static void displayPattern(int size, char ch)
{
for(int i = size; i >= 1; i-- )
{
int j;
for( j = 1; j < i; j++ )
{
System.out.print(" ");
}
for(int k=size; k >= j*2; k-- )
{
System.out.print(ch);
}
System.out.println();
}
for(int i = 2; i <= size; i++ )
{
System.out.print(" ");
int j;
for( j = 2; j < i; j++ )
{
System.out.print(" ");
}
for( int k = size; k >= j * 2; k-- )
{
System.out.print(ch);
}
System.out.println();
}
}
}
Explanation / Answer
Dear, Here is the modified code required for you. public class Stars { public static void displayPattern(int size, char ch) { for(int i = size; i >= 1; i-- ) { System.out.print(" "); int j; for( j = 1; j = j*2; k-- ) System.out.print(ch); System.out.println(); } for(int i = 1; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.