java 重复整数 n 次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34785312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Repeat an integer n times
提问by Aod Ren
I'm trying to make a pyramid out of an integer. I.E the number 3 :
我正在尝试用整数制作金字塔。IE数字3:
3
33
333
So based on the answers i found i made 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's not working and would be suboptimal. Is there a simple way to repeat an integer n times in the same line ?
但它不起作用,并且不是最理想的。有没有一种简单的方法可以在同一行中重复整数 n 次?
EDIT : made myself a method.. not quite happy either but it seems i can't just use something like System.out.println(int x, int n times)
编辑:让自己成为一个方法..也不太高兴,但似乎我不能只使用 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("");
}
采纳答案by Meera M Babu
I mean isn't it suboptimal to convert my int into a string ? AIn't there a direct way to deal with the integer ? –
我的意思是将我的 int 转换为字符串不是最理想的吗?没有处理整数的直接方法吗?——
If you dont want to convert int to string.
如果您不想将 int 转换为字符串。
This may help you.
这可能对你有帮助。
int n = 3;
for (int i=1; i<=n; i++) {
System.out.println(new String(new char[i]).replace("IntStream.range(1,n).forEach(i -> System.out.println(StringUtils.repeat(n2, i));
", n+""));
}
回答by Andremoniy
Ok, you cando this without explicitloops using Java-8
streams:
好的,您可以使用流在没有显式循环的情况下执行此操作Java-8
:
IntStream.range(0,n).forEach(i -> System.out.println(String.join("", Collections.nCopies(i+1, n2))));
or even without apache-commons
:
甚至没有apache-commons
:
public class Test{
public static String repeat(String str, int times) {
return new String(new char[times]).replace("3
33
333
3333
", str);
}
public static void main(String[] args) {
for (int i = 1; i < 5; i++) {
System.out.println(repeat("3", i));
}
}
}
But in any case internally all these methods use loops.
但无论如何在内部所有这些方法都使用循环。
回答by Ankur Singhal
Something as below.
如下。
int n = 8;
String n2 = Integer.toString(n);
StringBuilder builder = new StringBuilder(n);
for(int i = 0; i < n; i++) {
builder.append(n2);
System.out.println(builder.toString());
}
Output
输出
char[] str = new char[n];
Arrays.fill(str, (char)(number + '0'));
new String(str);
回答by Erik Nystr?m
You could try to use a StringBuilder.
您可以尝试使用StringBuilder。
You would still have to loop, but it might be slightly better performance-wise.
您仍然需要循环,但在性能方面可能会稍微好一些。
int lastValue = 0;
for (int i=0; i<=n; i++) {
int currentValue = lastValue + (n * Math.pow(10, i));
System.out.println(currentValue);
lastValue = currentValue ;
}
This does what you want, but not in the way you think about it. Instead of repeatedly having to create the repeating integer string, we simply build ONE string, saving us the work of repeating it.
这会做你想要的,但不是你想的那样。我们不必重复创建重复的整数字符串,而是简单地构建一个字符串,从而节省了重复它的工作。
To actually answer your question, you could use this code, although I would recomend the first approach:
要实际回答您的问题,您可以使用此代码,但我会推荐第一种方法:
##代码##This would only work if your integer is 0 <= number < 10.
这仅在您的整数为 0 <= number < 10 时才有效。
回答by Arnaud
To keep with integers, you may store last value each time, and add the next part :
为了保持整数,您可以每次存储最后一个值,并添加下一部分:
i = 0 --> you get 3
我 = 0 --> 你得到 3
i = 1 --> you get 33 (i0 + 30)
i = 1 --> 你得到 33 (i0 + 30)
i = 2 --> you get 333 (i1 + 300)
i = 2 --> 你得到 333 (i1 + 300)
##代码##This obviously works for one-digit integers only.
这显然只适用于一位数的整数。