我必须以表格格式打印出Ascii码(每行10个字符.)
现在我已经把它们全部打印好了。但是我想打印10个字符然后打印另外10个..。
我相信我应该能够用if (如果有10个字符,println.)语句来做这件事,但是我似乎不知道怎么做的逻辑。
请帮帮我..。
到目前为止我的代码是:
public class Ascii {
public static void main (String[]args) {
for (int c=32; c<123; c++) {
System.out.print((char)c);
// if(
//System.out.println();
}
}
}发布于 2015-10-09 11:38:10
利用模块化运算符%每10个字符添加一行:
public static void main(String[] args) {
for (int c = 32; c < 123; c++) {
System.out.print((char) c);
if ((c - 31) % 10 == 0) {
System.out.println();
}
}
}输出:
!"#$%&'()
*+,-./0123
456789:;<=
>?@ABCDEFG
HIJKLMNOPQ
RSTUVWXYZ[
\]^_`abcde
fghijklmno
pqrstuvwxy
z发布于 2015-10-09 11:37:14
这里有个条件应该能起作用。
if((c - 31) % 10 == 0) { System.out.println(); }发布于 2015-10-09 11:38:37
只需使用counter来跟踪位置。每当counter可被10除时,添加一个new line
int count = 0;
for (int c = 32; c < 123; c++) {
System.out.print((char)c);
count++;
if(count % 10 == 0)
System.out.println();
}https://stackoverflow.com/questions/33037227
复制相似问题