英文:
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 = {{'A', 'B', 'C', 'D'},{'E', 'F', 'G', 'H'},{'I', 'J', 'K', 'L'},{'M', 'N', 'O', 'P'}};
final int rowSize = 4;
final int columnSize = 4;
String[][] a = new String[rowSize][columnSize];
// iterate
for (int row = 0; row < rowSize; row++) {
for (int column = 0; column < columnSize; column++) {
a[row][column] = Char.valueOf(ar.charAt(rowSize * row + column));
}
}
// test
for (int row = 0; row < rowSize; row++) {
for (int column = 0; column < columnSize; column++) {
System.out.print(a[row][column] + " ");
}
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 'table A B ...' in output, you can do this:
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();
}
}
}
output:
------------
A B C D
E F G H
I J K L
M N O P
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论