转置 Java 中的矩阵但遇到错误
Transposing a matrix in Java but encountering errors
我必须确保我读入了一个文本文件(输入)并转置矩阵,从而使行变成列。
示例输入:
2 4
abcd/
efgh
(其中 2 表示行,4 表示列,/ 表示换行)
输出应如下所示:
ae/
bf/
cg/
dh
这是我的代码:
import java.util.*;
public class Transpose {
private void run() {
Scanner scan=new Scanner(System.in);
int Row= scan.nextInt();
int Col=scan.nextInt();
char[][] arr=new char[Row][Col];
for(int i=0;i<Row;i++){
for(int j=0;j<Col;j++){
arr[i][j]=scan.next().charAt(0);
}
}
for(int j=0;j<Col;j++){
for(int i=0;i<Row;i++){
System.out.print(arr[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
Transpose newTranspose = new Transpose();
newTranspose.run();
}
}
但是,我收到错误消息:
程序 crashed/producing 非零退出代码
这是运行时错误吗?我该如何解决这个问题。
这应该有效
public class Transpose {
private void run() {
Scanner scan = new Scanner(System.in);
System.out.println("Row");
int Row = scan.nextInt();
System.out.println("Col");
int Col = scan.nextInt();
char[][] arr = new char[Row][Col];
for (int i = 0; i < Row; i++) {
for (int j = 0; j < Col; j++) {
System.out.println("Enter character");
arr[i][j] = scan.next().charAt(0);
}
}
for (int j = 0; j < Col; j++) {
for (int i = 0; i < Row; i++) {
System.out.println(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Transpose newTranspose = new Transpose();
newTranspose.run();
}
}
具有所需的输出
试试这个。
Scanner scan = new Scanner(System.in);
int Row = scan.nextInt();
int Col = scan.nextInt();
scan.nextLine(); // skip newline.
char[][] arr = new char[Row][Col];
for (int i = 0; i < Row; i++) {
String line = scan.nextLine();
for (int j = 0; j < Col; j++) {
arr[i][j] = line.charAt(j);
}
}
for (int j = 0; j < Col; j++) {
for (int i = 0; i < Row; i++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
输入:
2 4
abcd
efgh
输出:
ae
bf
cg
dh
我必须确保我读入了一个文本文件(输入)并转置矩阵,从而使行变成列。
示例输入:
2 4
abcd/
efgh
(其中 2 表示行,4 表示列,/ 表示换行) 输出应如下所示:
ae/
bf/
cg/
dh
这是我的代码:
import java.util.*;
public class Transpose {
private void run() {
Scanner scan=new Scanner(System.in);
int Row= scan.nextInt();
int Col=scan.nextInt();
char[][] arr=new char[Row][Col];
for(int i=0;i<Row;i++){
for(int j=0;j<Col;j++){
arr[i][j]=scan.next().charAt(0);
}
}
for(int j=0;j<Col;j++){
for(int i=0;i<Row;i++){
System.out.print(arr[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
Transpose newTranspose = new Transpose();
newTranspose.run();
}
}
但是,我收到错误消息: 程序 crashed/producing 非零退出代码 这是运行时错误吗?我该如何解决这个问题。
这应该有效
public class Transpose {
private void run() {
Scanner scan = new Scanner(System.in);
System.out.println("Row");
int Row = scan.nextInt();
System.out.println("Col");
int Col = scan.nextInt();
char[][] arr = new char[Row][Col];
for (int i = 0; i < Row; i++) {
for (int j = 0; j < Col; j++) {
System.out.println("Enter character");
arr[i][j] = scan.next().charAt(0);
}
}
for (int j = 0; j < Col; j++) {
for (int i = 0; i < Row; i++) {
System.out.println(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Transpose newTranspose = new Transpose();
newTranspose.run();
}
}
具有所需的输出
试试这个。
Scanner scan = new Scanner(System.in);
int Row = scan.nextInt();
int Col = scan.nextInt();
scan.nextLine(); // skip newline.
char[][] arr = new char[Row][Col];
for (int i = 0; i < Row; i++) {
String line = scan.nextLine();
for (int j = 0; j < Col; j++) {
arr[i][j] = line.charAt(j);
}
}
for (int j = 0; j < Col; j++) {
for (int i = 0; i < Row; i++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
输入:
2 4
abcd
efgh
输出:
ae
bf
cg
dh