非法的 char 初始化程序 — 如何修复?

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

illegal initializer for char — how to fix?

问题

我想指定打印字符,但我在这里遇到了一个错误。我应该怎么办?(见我的代码)我只是自学的Java... 差不多一个月了。

public class l2 {
    public static void main(String[] args) {
    
        char[][] table = {{'A', 'B', 'C', 'D'}, {'E', 'F', 'G', 'H'}, {'I', 'J', 'K', 'L'}, {'M', 'N', 'O', 'P'}};
        final int rowSize = 4;
        final int columnSize = 4;
        char[][] a = new char[rowSize][columnSize];
        // 迭代
        for (int row = 0; row < rowSize; row++) {
            for (int column = 0; column < columnSize; column++) {
                a[row][column] = table[row][column];
            }
        }
        // 测试
        for (int row = 0; row < rowSize; row++) {
            for (int column = 0; column < columnSize; column++) {
                System.out.print(a[row][column] + " ");
            }
            System.out.println();
        }
    }
}

错误信息:

l2.java:4: 错误: char 的初始化程序非法
char[][] table = {{'A', 'B', 'C', 'D'}, {'E', 'F', 'G', 'H'}, {'I', 'J', 'K', 'L'}, {'M', 'N', 'O', 'P'}};

l2.java:7: 错误: 找不到符号 Char[][] a = new Char[rowSize][columnSize];
^ 符号: 类 Char 位置: 类 l2

l2.java:7: 错误: 找不到符号 Char[][] a = new Char[rowSize][columnSize];
^ 符号: 类 Char 位置: 类 l2

l2.java:11: 错误: 找不到符号 a[row][column] = Char.valueOf(ar.charAt(rowSize * row + column));
英文:

I want to specify print characters, but I had an error here. What should I do? (See my code) I'm just self taught in Java... almost a month now.

public class l2{
public static void main(String[] args){

	char table = {{&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;},{&#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;},{&#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;},{&#39;M&#39;, &#39;N&#39;, &#39;O&#39;, &#39;P&#39;}};
    final int rowSize = 4;
    final int columnSize = 4;
    String[][] a = new String[rowSize][columnSize];
    // iterate
    for (int row = 0; row &lt; rowSize; row++) {
        for (int column = 0; column &lt; columnSize; column++) {
            a[row][column] = Char.valueOf(ar.charAt(rowSize * row + column));
        }
    }
    // test
    for (int row = 0; row &lt; rowSize; row++) {
        for (int column = 0; column &lt; columnSize; column++) {
            System.out.print(a[row][column] + &quot; &quot;);
        }
        System.out.println();
    }
}
}

The errors are:

> l2.java:4: error: illegal initializer for char
> char table = {{'A', 'B', 'C', 'D'},{'E', 'F', 'G', 'H'},{'I', 'J', 'K', 'L'},{'M', 'N', 'O', 'P'}};
>
> l2.java:7: error: cannot find symbol Char[][] a = new Char[rowSize][columnSize];
> ^ symbol: class Char location: class l2
> l2.java:7: error: cannot find symbol Char[][] a = new Char[rowSize][columnSize];
> ^ symbol: class Char location: class l2
> l2.java:11: error: cannot find symbol a[row][column] = Char.valueOf(ar.charAt(rowSize * row + column));

答案1

得分: 1

如果您的目标是在输出中打印 'table A B ...',您可以这样做

public class MyClass{

    public static void main(String[] args) {

        char[][] table = {{'A', 'B', 'C', 'D'}, {'E', 'F', 'G', 'H'}, {'I', 'J', 'K', 'L'}, {'M', 'N', 'O', 'P'}};
        final int rowSize = 4;
        final int columnSize = 4;

        for (int row = 0; row < rowSize; row++) {
            for (int column = 0; column < columnSize; column++) {
                System.out.print(table[row][column] + " ");
            }
            System.out.println();
        }
    }
}

输出:

A B C D 
E F G H 
I J K L 
M N O P

<details>
<summary>英文:</summary>

If your goal is to print &#39;table A B ...&#39; in output, you can do this:

    public class MyClass{
    
        public static void main(String[] args) {
    
            char[][] table = {{&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;}, {&#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;}, {&#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;}, {&#39;M&#39;, &#39;N&#39;, &#39;O&#39;, &#39;P&#39;}};
            final int rowSize = 4;
            final int columnSize = 4;
            
            for (int row = 0; row &lt; rowSize; row++) {
                for (int column = 0; column &lt; columnSize; column++) {
                    System.out.print(table[row][column] + &quot; &quot;);
                }
                System.out.println();
            }
        }
    } 

output:
------------

    A B C D 
    E F G H 
    I J K L 
    M N O P 

</details>



huangapple
  • 本文由 发表于 2020年9月19日 14:55:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63966111.html
匿名

发表评论

匿名网友

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

确定