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

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

Transposing a matrix in Java but encountering errors

问题

以下是翻译好的代码部分:

  1. import java.util.*;
  2. public class Transpose {
  3. private void run() {
  4. Scanner scan = new Scanner(System.in);
  5. int Row = scan.nextInt();
  6. int Col = scan.nextInt();
  7. char[][] arr = new char[Row][Col];
  8. for (int i = 0; i < Row; i++) {
  9. for (int j = 0; j < Col; j++) {
  10. arr[i][j] = scan.next().charAt(0);
  11. }
  12. }
  13. for (int j = 0; j < Col; j++) {
  14. for (int i = 0; i < Row; i++) {
  15. System.out.print(arr[i][j]);
  16. }
  17. System.out.println();
  18. }
  19. }
  20. public static void main(String[] args) {
  21. Transpose newTranspose = new Transpose();
  22. newTranspose.run();
  23. }
  24. }

请注意,由于您要求只返回翻译后的代码部分,我已经将注释和非代码内容省略。如果您对翻译的代码有任何问题或需要进一步解释,请随时提问。

英文:

I have to ensure that I read in a text file(input) and transpose the matrix whereby the rows become columns.

Sample input:

  1. 2 4
  2. abcd/
  3. efgh

(whereby 2 indicates row and 4 indicates column and / indicates new line)
and output should look like:

  1. ae/
  2. bf/
  3. cg/
  4. dh

This is my code:

  1. import java.util.*;
  2. public class Transpose {
  3. private void run() {
  4. Scanner scan=new Scanner(System.in);
  5. int Row= scan.nextInt();
  6. int Col=scan.nextInt();
  7. char[][] arr=new char[Row][Col];
  8. for(int i=0;i&lt;Row;i++){
  9. for(int j=0;j&lt;Col;j++){
  10. arr[i][j]=scan.next().charAt(0);
  11. }
  12. }
  13. for(int j=0;j&lt;Col;j++){
  14. for(int i=0;i&lt;Row;i++){
  15. System.out.print(arr[i][j]);
  16. }
  17. System.out.println();
  18. }
  19. }
  20. public static void main(String[] args) {
  21. Transpose newTranspose = new Transpose();
  22. newTranspose.run();
  23. }
  24. }

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

  1. 这应该可以工作
  2. public class Transpose {
  3. private void run() {
  4. Scanner scan = new Scanner(System.in);
  5. System.out.println("行数");
  6. int 行数 = scan.nextInt();
  7. System.out.println("列数");
  8. int 列数 = scan.nextInt();
  9. char[][] arr = new char[行数][列数];
  10. for (int i = 0; i < 行数; i++) {
  11. for (int j = 0; j < 列数; j++) {
  12. System.out.println("输入字符");
  13. arr[i][j] = scan.next().charAt(0);
  14. }
  15. }
  16. for (int j = 0; j < 列数; j++) {
  17. for (int i = 0; i < 行数; i++) {
  18. System.out.print(arr[i][j] + " ");
  19. }
  20. System.out.println();
  21. }
  22. }
  23. public static void main(String[] args) {
  24. Transpose newTranspose = new Transpose();
  25. newTranspose.run();
  26. }
  27. }
  28. 带有期望输出
英文:

This should work

  1. public class Transpose {
  2. private void run() {
  3. Scanner scan = new Scanner(System.in);
  4. System.out.println(&quot;Row&quot;);
  5. int Row = scan.nextInt();
  6. System.out.println(&quot;Col&quot;);
  7. int Col = scan.nextInt();
  8. char[][] arr = new char[Row][Col];
  9. for (int i = 0; i &lt; Row; i++) {
  10. for (int j = 0; j &lt; Col; j++) {
  11. System.out.println(&quot;Enter character&quot;);
  12. arr[i][j] = scan.next().charAt(0);
  13. }
  14. }
  15. for (int j = 0; j &lt; Col; j++) {
  16. for (int i = 0; i &lt; Row; i++) {
  17. System.out.println(arr[i][j] + &quot; &quot;);
  18. }
  19. System.out.println();
  20. }
  21. }
  22. public static void main(String[] args) {
  23. Transpose newTranspose = new Transpose();
  24. newTranspose.run();
  25. }
  26. }

With desired output

答案2

得分: 0

试试这个。

  1. Scanner scan = new Scanner(System.in);
  2. int Row = scan.nextInt();
  3. int Col = scan.nextInt();
  4. scan.nextLine(); // 跳过换行符。
  5. char[][] arr = new char[Row][Col];
  6. for (int i = 0; i < Row; i++) {
  7. String line = scan.nextLine();
  8. for (int j = 0; j < Col; j++) {
  9. arr[i][j] = line.charAt(j);
  10. }
  11. }
  12. for (int j = 0; j < Col; j++) {
  13. for (int i = 0; i < Row; i++) {
  14. System.out.print(arr[i][j]);
  15. }
  16. System.out.println();
  17. }

输入:

  1. 2 4
  2. abcd
  3. efgh

输出:

  1. ae
  2. bf
  3. cg
  4. dh
英文:

Try this.

  1. Scanner scan = new Scanner(System.in);
  2. int Row = scan.nextInt();
  3. int Col = scan.nextInt();
  4. scan.nextLine(); // skip newline.
  5. char[][] arr = new char[Row][Col];
  6. for (int i = 0; i &lt; Row; i++) {
  7. String line = scan.nextLine();
  8. for (int j = 0; j &lt; Col; j++) {
  9. arr[i][j] = line.charAt(j);
  10. }
  11. }
  12. for (int j = 0; j &lt; Col; j++) {
  13. for (int i = 0; i &lt; Row; i++) {
  14. System.out.print(arr[i][j]);
  15. }
  16. System.out.println();
  17. }

input:

  1. 2 4
  2. abcd
  3. efgh

output:

  1. ae
  2. bf
  3. cg
  4. 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:

确定