Java 新手问个问题, System.out.println("##############################")怎样快速的打出这 30 个"#"啊?

2017-04-22 16:48:04 +08:00
 t2doo
System.out.println("##############################");
怎样快速的打出这 30 个"#"啊,在 php 中可以用 str_repeat("#", 30),在 python 中直接 print("#" * 30),那在 Java 中要怎么写呢?
3Q
3584 次点击
所在节点    问与答
18 条回复
hjc4869
2017-04-22 16:51:41 +08:00
Infernalzero
2017-04-22 16:52:29 +08:00
StringUtils.repeat("#", 30)
下一题
Cbdy
2017-04-22 17:14:27 +08:00
@Infernalzero 这个要第三方库吧?

用 stream api
Stream.generate(() -> "#").limit(30).forEach(System.out::print);
t2doo
2017-04-22 17:17:16 +08:00
System.out.println(String.join("", Collections.nCopies(30, "#")));
sqsgalaxys
2017-04-22 17:21:32 +08:00
vim 中 Normal 模式下: `30 i # esc`
loveuqian
2017-04-22 17:58:17 +08:00
print #
print #
print #
print #
print #
。。。
scnace
2017-04-22 18:14:15 +08:00
这个简单 我用 Poker 把#键换成 30 个#就可以了🙈
springmarker
2017-04-22 18:24:20 +08:00
for 循环,哈哈
kyuuseiryuu
2017-04-22 18:54:05 +08:00
public String repeat(char c, int repeat){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < repeat; i++){
sb.append(c);
}
return sb.toString();
}


System.out.println(repeat('#',30));
xdz0611
2017-04-22 18:57:15 +08:00
python 还是比较爽啊 ,直接 * 30 就可以了.
FunctionOne
2017-04-22 20:24:54 +08:00
留言中可以用 markDown 各式代码么?
whileFalse
2017-04-22 20:46:50 +08:00
public static final String SHARP30 = "##############################"

System.out.println(SHARP30);
wineway
2017-04-22 22:07:06 +08:00
@xdz0611 scala 也可以😗楼主快语言!
wancaibida
2017-04-22 22:31:25 +08:00
用 groovy
Pastsong
2017-04-22 22:43:16 +08:00
@whileFalse 这个是 hash ... C Sharp 是借用乐谱符号里的♯
winterbells
2017-04-22 22:52:34 +08:00
int[] i = new int[30];
for (int j : i){
System.out.print("#");
}
Mutoo
2017-04-22 23:01:46 +08:00
把它存到 live template 或者 snippet ,然后随时取用。
ewBuyVmLZMZE
2017-04-23 02:42:02 +08:00
@Cbdy 用 IntStream 更好

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/356655

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX