如何打印一个字符数组的二维数组char[][]?

huangapple go评论127阅读模式
英文:

How do I print a 2D array of char[][]?

问题

我创建了一个char[][]的二维数组。我想使用ASCII码打印小写字母。
我编写了代码,但我没有得到预期的结果。我错在哪里?

我知道如果我像answercode一样修复代码,它可以正常工作。但我想知道为什么当我使用我的代码时什么都不会发生,即使控制台中没有a

这是我的代码:

  1. package array;
  2. public class Alphabet {
  3. public static void main(String[] args) {
  4. char[][] alphabets = new char[13][2];
  5. alphabets[0][0] = 'a';
  6. for (int i = 0; i < alphabets.length; i++) {
  7. for (int j = 0; j < alphabets[i].length; j++) {
  8. System.out.print(alphabets[i][j]);
  9. alphabets[i][j]++;
  10. }
  11. System.out.println();
  12. }
  13. }
  14. }

预期输出是:

  1. a b
  2. c d
  3. e f
  4. ...
  5. ...
  6. y z
英文:

I made 2 dimensional array of char[][]. I want to print small letter by using ASCII code.
I wrote the code, but I don't have the expected results. Where I'm wrong?

I know if I fix code like answercode it works well. But I want to know why nothing happens when I use my code, even if there are no a in console.

Here is my code:

  1. package array;
  2. public class Alphabet {
  3. public static void main(String[] args) {
  4. char[][] alphabets = new char[13][2];
  5. alphabets[0][0] = &#39;a&#39;;
  6. for (int i = 0; i &lt; alphabets.length; i++) {
  7. for (int j = 0; j &lt; alphabets[i].length; j++) {
  8. System.out.print(alphabets[i][j]);
  9. alphabets[i][j]++;
  10. }
  11. System.out.println();
  12. }
  13. }
  14. }

The expected output is:

  1. a b
  2. c d
  3. e f
  4. ...
  5. ...
  6. y z

答案1

得分: 0

您的代码完美运行,但您看不到字母,因为您没有将所有的char放入char[][]中。

所以,您可以创建一个简单的名为populate的方法,如下所示:

  1. private static void populate(char[][] alphabets) {
  2. char c = 'a';
  3. for (int i = 0; i < alphabets.length; i++) {
  4. for (int j = 0; j < alphabets[i].length; j++) {
  5. if (c <= 'z') { //我们想要从'a'到'z'
  6. alphabets[i][j] = c;
  7. c++; //获取下一个字母
  8. }//if
  9. }//for
  10. }//for
  11. }//populate

您可以调用这个方法,而不是alphabets[0][0] = 'a';。所以主要的代码将是:

  1. public static void main(String[] args) {
  2. //创建char[][]
  3. char[][] alphabets = new char[13][2];
  4. //填充char[][]
  5. populate(alphabets);
  6. //打印输出
  7. for (int i = 0; i < alphabets.length; i++) {
  8. for (int j = 0; j < alphabets[i].length; j++) {
  9. System.out.print(alphabets[i][j]);
  10. alphabets[i][j]++;
  11. }
  12. System.out.println();
  13. }
  14. }

这是输出结果(我在jdoodle上测试过):

如何打印一个字符数组的二维数组char[][]?

英文:

Your code works perfectly, but you can't see the alphabet because you haven't put all the char in the char[][].

So, you can create a simple method called populate, like this:

  1. private static void populate(char[][] alphabets) {
  2. char c = &#39;a&#39;;
  3. for (int i = 0; i &lt; alphabets.length; i++) {
  4. for (int j = 0; j &lt; alphabets[i].length; j++) {
  5. if (c &lt;= &#39;z&#39;) { //we want from &#39;a&#39; to &#39;z&#39;
  6. alphabets[i][j] = c;
  7. c++; //get the next letter
  8. }//if
  9. }//for
  10. }//for
  11. }//populate

You can call this method instead of alphabets[0][0] = &#39;a&#39;;. So the Main code will be:

  1. public static void main(String[] args) {
  2. //Create char[][]
  3. char[][] alphabets = new char[13][2];
  4. //populate char[][]
  5. populate(alphabets);
  6. //print the output
  7. for (int i = 0; i &lt; alphabets.length; i++) {
  8. for (int j = 0; j &lt; alphabets[i].length; j++) {
  9. System.out.print(alphabets[i][j]);
  10. alphabets[i][j]++;
  11. }
  12. System.out.println();
  13. }
  14. }

This is the output (I've tested it on jdoodle):

如何打印一个字符数组的二维数组char[][]?

huangapple
  • 本文由 发表于 2020年7月28日 18:43:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63132282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定