Java 在代码实现后,如何去掉方括号?
我做这个问题是为了一个任务,我们必须一个元素一个元素地添加两个矩阵,这就是输出:Java 在代码实现后,如何去掉方括号?,java,Java,我做这个问题是为了一个任务,我们必须一个元素一个元素地添加两个矩阵,这就是输出: [0] [1] [2] [3] [0] 14 14 15 11 [1] 8 9 14 11 [2] 10 18 7 16 [3] 4 12 11 8 我的主要问题是如何去掉显示在矩阵顶部和左侧的方括号。代码如下: package question1; import java.util.Scanner; public class Questio
[0] [1] [2] [3]
[0] 14 14 15 11
[1] 8 9 14 11
[2] 10 18 7 16
[3] 4 12 11 8
我的主要问题是如何去掉显示在矩阵顶部和左侧的方括号。代码如下:
package question1;
import java.util.Scanner;
public class Question1 {
public static void initialize(int[][] a) {
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[0].length; j++){
a[i][j] = (int)Math.ceil(Math.random() * 9);
System.out.print(a[i][j] + "\t");
}
System.out.print("\n");
}
}
public static int[][] sum(int[][] a, int[][] b) {
int[][] result = new int[a.length][a[0].length];
for(int row = 0; row<a.length; row++){
for(int col = 0; col<a[row].length; col++){
result[row][col] = a[row][col] + b[row][col];
}
}
return result;
}
public static void printArray(int[][] a) {
System.out.println("\t");
for (int col=0; col<a[0].length; col++) {
System.out.print("[" + col + "]\t");
}
System.out.println();
for (int row=0; row<a.length; row++) {
System.out.print("[" + row + "]\t");
for (int col=0; col<a[row].length; col++) {
System.out.print(a[row][col] + "\t");
}
System.out.println();
}
}
public static void main(String[] args){
Scanner keyb = new Scanner(System.in);
System.out.println("Enter the rows and columns for the first matrix (row followed by columns):");
int row1 = keyb.nextInt();
int col1 = keyb.nextInt();
int[][] c1 = new int[row1][col1];
System.out.println("Your first matrix is:");
System.out.println("-------------------------------");
initialize(c1);
System.out.println("-------------------------------");
System.out.println("Enter the rows and columns for the second matrix (row followed by columns):");
int row2 = keyb.nextInt();
int col2 = keyb.nextInt();
int[][] c2 = new int[row2][col2];
System.out.println("Your second matrix is:");
System.out.println("-------------------------------");
initialize(c2);
System.out.println("-------------------------------");
System.out.println();
System.out.println("The sum of your arrays are:");
System.out.println("-------------------------------");
int [][] result;
result = sum(c1, c2);
printArray(result);
System.out.println("-------------------------------");
}
}
package问题1;
导入java.util.Scanner;
公开课问题1{
公共静态无效初始化(int[]a){
对于(inti=0;i 对于(int row=0;row只需修改printArray()
方法,使用空格代替列标题和行标题的括号:
public static void printArray(int[][] a) {
System.out.println("\t");
for (int col=0; col < a[0].length; col++) {
System.out.print(" " + col + " \t"); // use spaces instead of brackets
}
System.out.println();
for (int row=0; row < a.length; row++) {
System.out.print(" " + row + " \t"); // use spaces not brackets
for (int col=0; col < a[row].length; col++) {
System.out.print(a[row][col] + "\t");
}
System.out.println();
}
}
publicstaticvoidprintary(int[]a){
System.out.println(“\t”);
for(int col=0;col
家庭作业问题在这里是不受欢迎的,但我会给你一个提示…想想哪些语句会打印输出到控制台…@讽刺的是,只要是关于主题的家庭作业问题就完全可以了。在这种情况下,OP对解决他所面临的问题几乎没有什么帮助。如果你想擅长编程,也许可以学习代码,请尝试。这几乎太明显了。请去掉像System.out.print(“[”+col+“]\t”);
和System.out.print(“[”+row+“]\t”);