public class Student {
public static void main(String[] args) {
Object[][] array2d = new Object[3][7];
array2d[0][0] = "student name";
array2d[0][1] = "firstest";
array2d[0][2] = "secondtest ";
array2d[0][3] = "schoolworks";
array2d[0][4] = "homeworks";
array2d[0][5] = "finaltest";
array2d[0][6] = "total grade";
array2d[1][0] = "jack";
array2d[1][1] = new Integer(15);
array2d[1][2] = new Integer(10);
array2d[1][3] = new Integer(7);
array2d[1][4] = new Integer(8);
array2d[1][5] = new Integer(35);
array2d[1][6] = new Integer(75);
array2d[2][0] = "john";
array2d[2][1] = new Integer(19);
array2d[2][2] = new Integer(15);
array2d[2][3] = new Integer(9);
array2d[2][4] = new Integer(5);
array2d[2][5] = new Integer(30);
array2d[2][6] = new Integer(78);
for (int row = 0; row < array2d.length; row++) {
for (int colums = 0; colums < array2d[row].length; colums++)
System.out.println(array2d[row][colums]);
}
我想在此订单上打印2d混合数组String和int。表 新的java和网站熊我
student name firstest secondtest schoolworks homeworks finaltest total grade
jack 15 10 8 7 35 67
按此订单打印表格 用我糟糕的英语和java编辑尽可能多的
答案 0 :(得分:1)
要打印出一些看起来像你在帖子中显示的内容需要一些思考,并涉及很多步骤。这是让你入门的东西:
//First we need to set a spacing for each item,
//else the table will look bad and not be in line:
int spacing = 14;
//Now loop through the array and store each item in `rowToPrint`
//The for loop will repeat for each row
for (int row = 0; row < array2d.length; row++)
{
//Create a string to store a whole row for printing:
String rowToPrint = "";
//The for loop will repeat for each column
for (int column = 0; column < array2d[row].length; column++)
{
//get previous row items
String previousItems = rowToPrint;
//get next array item
String newItem = array2d[row][column];
//work out array item length
int length = array2d[row][column].length();
//work out padding for this item by doing (Spacing - array item length), and add blank spaces.
//Example: 14(spacing) - 8 (item length) = 6 spaces of padding
String padding = String.format("%" + (spacing - length) + "s", "");
//now add add it all together
rowToPrint = previousItems + newItem + padding;
}
//Now print current row in full with spacing/padding
System.out.println(rowToPrint);
}
这段代码可能更简单,但我保持分离和清晰,以便您可以轻松查看正在发生的事情并轻松进行更改。
您还需要更改部分代码,否则示例将无效:
从此Object[][] array2d = new Object[3][7];
更改为此String[][] array2d = new String[3][7];
,因为这将无效.length()
,并且需要更多代码才能处理它。
也不要指定像这个array2d[1][1] = new Integer(15);
这样的整数,而是像array2d[1][1] = "15";
那样或array2d[1][1] = "" + new Integer(15);
。如果您需要将数据转换回整数,请使用以下内容:Integer.parseInt(array2d[1][1])
上述代码的示例输出:
student name firstest secondtest schoolworks homeworks finaltest total grade
jack 15 10 7 8 35 75
john 19 15 9 5 30 78
答案 1 :(得分:0)
以上代码有几个问题:
您的2D数组矩阵,当打印循环对象很奇怪时,请使用int作为索引。
public class Test{
public static void main(String[] args)
{
Object[][] array2d = new Object[3][7];
array2d[0][0] = "اسم الطالب";
array2d[0][1] = "اختبارالاول";
array2d[0][2] = "اختبارالثاني";
array2d[0][3] = "المشاركه";
array2d[0][4] = "الواجبات";
array2d[0][5] = "الاختبار النهائي";
array2d[0][6] = "المجموع";
array2d[1][0] = "عبدالله";
array2d[1][1] = new Integer(15);
array2d[1][2] = new Integer(10);
array2d[1][3] = new Integer(7);
array2d[1][4] = new Integer(8);
array2d[1][5] = new Integer(35);
array2d[1][6] = new Integer(75);
array2d[1][0] = "محمد"; // Not sure if you should be reassigning the array elements, although you can
array2d[1][1] = new Integer(19);
array2d[1][2] = new Integer(15);
array2d[1][3] = new Integer(9);
array2d[1][4] = new Integer(5);
array2d[1][5] = new Integer(30);
array2d[1][6] = new Integer(78);
for (int row = 0; row < array2d.length; row++)
{
for (int colums = 0; colums < array2d[row].length; colums++)
System.out.println(array2d[row][colums]);
}
}}