无法通过2维java阵列打印字符
我试着把这段代码作为一个分数表来运行,其中我的标题有chars和int,如下所示无法通过2维java阵列打印字符,java,arrays,Java,Arrays,我试着把这段代码作为一个分数表来运行,其中我的标题有chars和int,如下所示 A B c 1 2 65 //this is where I'm stuck again! 3 为了在特定位置(矩阵)打印上面(65)中的分数,但一旦我尝试添加打印语句,表就会崩溃。任何帮助都将不胜感激 public class Table3 { static int[][] list = new int[4][4]; //private cha
A B c
1
2 65 //this is where I'm stuck again!
3
为了在特定位置(矩阵)打印上面(65)中的分数,但一旦我尝试添加打印语句,表就会崩溃。任何帮助都将不胜感激
public class Table3 {
static int[][] list = new int[4][4];
//private char column = 'A';
//private int row = 1;
private static int row = 1;
public Table3(){
//column = 'A';
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 4; j++)
list[i][j] = 0;
}
}
public static void table(char col, int row, int value) {
//System.out.printf("\n\n%s\n", "Table");
for (int i = 1; i < 4; i++) {
System.out.print(row + " ");
row++;
for (int j = 1; j < 4; j++)System.out.print(col + " ");
System.out.println("\n");
col++;
if (row >= 0 && row <= 4 && col >=0 && col <= 4)
System.out.print(list[col][row]=value);
System.out.println("\n");
}
}
}
了解如何使用
System.out.printf
。这些文件是
公共类表3
{
静态int numRows=4;
静态int numCols=4;
静态int[][]列表=新int[numRows][numCols];
公开表3()
{
//列='A';
对于(int i=0;i<4;i++)
{
//这是行号,因此不必手动打印
//只需打印数组
列表[i][0]=i;
//将列表初始化为0
对于(int j=1;j<4;j++)
{
列表[i][j]=0;
}
}
}
公共静态无效表(字符列、整数行、整数值)
{
列表[行][列]=值;
int columnWidth=5;//以字符为单位
//第一列标题前的空格为空
对于(int i=0;i
您只能嵌套一行没有大括号的代码。没错,因此,无论我如何重置循环,表都会崩溃。必须有一种方法可以将值打印到表中的任何位置!请解决或不要使用智能注释。我在这方面有大学博士学位。打印方法正在覆盖调用,这是一个问题……所以不要重复请告诉我有关循环的事……解决它或保持安静!
public class TableTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Table3 t = new Table3();
t.table('A', 5, 5);
}
}
public class Table3
{
static int numRows = 4;
static int numCols = 4;
static int[][] list = new int[numRows][numCols];
public Table3()
{
//column = 'A';
for (int i = 0; i < 4; i++)
{
//this is the row number so you don't have to print it manually
//just print the array
list[i][0] = i;
//initialize the list to 0
for (int j = 1; j < 4; j++)
{
list[i][j] = 0;
}
}
}
public static void table(char col, int row, int value)
{
list[row][col] = value;
int columnWidth = 5; //in characters
//empty space before first column header
for (int i = 0; i < columnWidth; i++)
{
System.out.print(" ");
}
//print the column headers (A through C)
for (int i = 1; i < numCols; i++)
{
System.out.printf("%-" + columnWidth" + "c", (char)(64 + i));
}
System.out.println(); //get off of the column header row
//print the rest of the table
for (int i = 1; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
if (list[i][j] == 0)
{
System.out.printf("%" + columnWidth + "s", " ");
}
else
{
System.out.printf("%-" + columnWidth + "d", list[i][j]);
}
}
System.out.println("\n");
}
}
}