0

I need your help! This is a java program about "Method" ,and i want to make a triangle pattern. Please have a look.Thank you!

import java.util.Scanner; public class gh{ public static void goodboy(char c,int i){ while(i-- >0); System.out.print(c); } public static void main(String[]args){ Scanner input=new Scanner(System.in); System.out.println("Let us display the level of the triangle!"); System.out.println(); System.out.println("What is the level of triangle?"); System.out.println(); System.out.print("n="); int n=input.nextInt(); System.out.println("The level of triangle : "+n); System.out.println(); for(int i=1;n>=i;i++){ goodboy(' ',n-1); goodboy('*',(i*2)-1); System.out.println(); } } } I want to make a triangle pattern by using "Method" in java. Like this one👇👇👇👇👇 * *** ***** *******

28th Jun 2018, 6:34 AM
Hunt
Hunt - avatar
5 Antworten
+ 2
Well, string is there only to make changing of symbol that makes the pyramid possible. I wanted to use char after all, but I forgot the name of scanner's method that reads next char, so I just used string of one character instead. And about your code, I laughed so hard when I found the error. You got trolled by the oldest enemy of all programmers - the semicolon. The semicolon after the while loop shouldn't be there. The semicolon is just executed x times and character is printed then only once. The second error is in your for loop. goodboy(' ',n-1); must be replaced with goodboy(' ',n-i);
28th Jun 2018, 1:39 PM
Jan Štěch
Jan Štěch - avatar
+ 1
Here you go. Let me know if you need any further explanation about how the program works. https://code.sololearn.com/cKbHPi42K707/?ref=app
28th Jun 2018, 7:12 AM
Jan Štěch
Jan Štěch - avatar
+ 1
Sorry i am late,i have not learned how to use "String".Thank you for your program. I do not know what is problem in my program.It seems normal,but i can not get the result like the example
28th Jun 2018, 1:13 PM
Hunt
Hunt - avatar
0
Hey friend you are right!
28th Jun 2018, 4:03 PM
Hunt
Hunt - avatar
0
I laughed too😂
28th Jun 2018, 4:05 PM
Hunt
Hunt - avatar