在Java中转置矩阵时遇到错误。

huangapple go评论74阅读模式
英文:

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&lt;Row;i++){
            for(int j=0;j&lt;Col;j++){
                arr[i][j]=scan.next().charAt(0);
            }
        }
        for(int j=0;j&lt;Col;j++){
            for(int i=0;i&lt;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(&quot;Row&quot;);
        int Row = scan.nextInt();
        System.out.println(&quot;Col&quot;);
        int Col = scan.nextInt();
        char[][] arr = new char[Row][Col];
        for (int i = 0; i &lt; Row; i++) {
            for (int j = 0; j &lt; Col; j++) {
                System.out.println(&quot;Enter character&quot;);
                arr[i][j] = scan.next().charAt(0);
            }
        }
        for (int j = 0; j &lt; Col; j++) {
            for (int i = 0; i &lt; Row; i++) {
                System.out.println(arr[i][j] + &quot; &quot;);
            }
            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 &lt; Row; i++) {
    String line = scan.nextLine();
    for (int j = 0; j &lt; Col; j++) {
        arr[i][j] = line.charAt(j);
    }
}
for (int j = 0; j &lt; Col; j++) {
    for (int i = 0; i &lt; Row; i++) {
        System.out.print(arr[i][j]);
    }
    System.out.println();
}

input:

2 4
abcd
efgh

output:

ae
bf
cg
dh

huangapple
  • 本文由 发表于 2020年8月22日 04:50:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63529903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定