java - ASCII 表中缺少字符 - Java

标签 java for-loop ascii

我用 Java 创建了一个 ASCII 表,每行打印 10 个字符,从“!”开始到‘~’。除了第一行只打印了九个字符(或者打印了一个空格?)之外,一切都运行良好。有人看到会导致此问题的语法或处理问题吗?这只发生在第一排。 注意:我只被允许使用一个 for 循环。

public class AsciiChars {

    public static void main(String[]args) {

        int count = 0; // initialize counter variable

        for (int ascii = 33; ascii < 127; ascii++, count++) { //conditions for the for loop

            if ((ascii - 12) % 10 == 0){ //formula needed to create the rows. The end char in each row,
                                         // minus 12 (42, 52, 62, etc), will be divisible by 10, thus ending the row.
            System.out.println("\n"); // Print a new line after every row.
        } //end if
            System.out.print((char)ascii + " "); //casting the ascii int to a char and adding a space after every char
        }//end for loop
    }//end main
}//end class

最佳答案

你的数学是正确的,因为它将决定在第 10 个字符 (#42) 上打印换行符。但是,在打印字符之前先打印换行符,因此只有 9 个字符到达第一行。第 10 到 19 个字符打印在第二行,依此类推。

将换行打印行和关联的 if 语句移到当前字符的打印之后。

此外,println 将在作为参数传递的字符串之后打印换行符。您只需调用println()即可。

关于java - ASCII 表中缺少字符 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28654802/

相关文章:

c - 我如何接收 "cat"作为我的程序参数?

java - Jsoup - 从 href 属性中选择值

java - 在android中显示当前音乐播放信息

java - appengine-mapper 库和 1.6.6 appengine-api-labs.jar 冲突

javascript - For循环已初始化但未进入

string - 如何连接字符串+我?

javascript - 如何使用selenium实现Java循环

java - 了解 for 循环和数组索引

python - 尝试使用 Python xlwt 保存 Excel 文件时出现 UnicodeDecodeError

python - 编译语法错误 : non ASCII letters in a string