Java – repeat once integer n times
•
Java
I tried to make a pyramid out of integers
3 33 333
So based on the answer I found, I did this:
int n = 8; String n2 = Integer.toString(n); for (int i=0; i<n; i++) { System.out.println(StringUtils.repeat(n2,i)); }
But it doesn't work and is not ideal Is there a simple way to repeat an integer n times in the same line?
Editor: make yourself a method Not very happy, but it seems that I can't just use something like system out. Println (int x, int n times)
int n = 8; for (int i=0; i<=n; i++) { for (int j=0; j<i; j++) { System.out.print(n + " "); } System.out.println(""); }
Solution
If you don't want to convert int to string
This may help you
int n = 3; for (int i=1; i<=n; i++) { System.out.println(new String(new char[i]).replace("\0",n+"")); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码