英文:
Transposing a matrix in Java but encountering errors
问题
以下是翻译好的代码部分:
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();
}
}
请注意,由于您要求只返回翻译后的代码部分,我已经将注释和非代码内容省略。如果您对翻译的代码有任何问题或需要进一步解释,请随时提问。
英文:
I have to ensure that I read in a text file(input) and transpose the matrix whereby the rows become columns.
Sample input:
2 4
abcd/
efgh
(whereby 2 indicates row and 4 indicates column and / indicates new line)
and output should look like:
ae/
bf/
cg/
dh
This is my code:
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();
}
}
However, I am getting the error:
programme crashed/producing non-zero exit code
Is this a runtime error and how can I go about fixing this.
答案1
得分: 0
这应该可以工作
public class Transpose {
private void run() {
Scanner scan = new Scanner(System.in);
System.out.println("行数");
int 行数 = scan.nextInt();
System.out.println("列数");
int 列数 = scan.nextInt();
char[][] arr = new char[行数][列数];
for (int i = 0; i < 行数; i++) {
for (int j = 0; j < 列数; j++) {
System.out.println("输入字符");
arr[i][j] = scan.next().charAt(0);
}
}
for (int j = 0; j < 列数; j++) {
for (int i = 0; i < 行数; i++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Transpose newTranspose = new Transpose();
newTranspose.run();
}
}
带有期望输出
英文:
This should work
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();
}
}
With desired output
答案2
得分: 0
试试这个。
Scanner scan = new Scanner(System.in);
int Row = scan.nextInt();
int Col = scan.nextInt();
scan.nextLine(); // 跳过换行符。
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
英文:
Try this.
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();
}
input:
2 4
abcd
efgh
output:
ae
bf
cg
dh
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论