package com.test;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
System.out.println("Rows = ?");
Scanner sc = new Scanner(System.in);
if(sc.hasNextInt()) {
int nrows = sc.nextInt();
System.out.println("Columns = ?");
if(sc.hasNextInt()) {
int ncolumns = sc.nextInt();
char matrix[][] = new char[nrows][ncolumns];
System.out.println("Enter matrix");
for (int row = 0; sc.hasNextLine() && nrows > row; row++) {
matrix[row] = sc.nextLine().toCharArray();
}
for (int row = 0; row < nrows; row++) {
for (int column = 0; column < matrix[row].length; column++) {
System.out.print(matrix[row][column] + "\t");
}
System.out.println();
}
}
}
}
}
所以我的程序讀取矩陣並打印它,但最後一行不打印。我認爲,for循環中的問題是打印列。Java Matrix不打印最後一行
輸入:
2
2
-=
=-
實際輸出:
-=
預期輸出:
-=
=-
您可以發佈輸入,實際輸出和預期輸出嗎?這可能是因爲你沒有在第一個for循環中正確地填充你的變量。 – 2014-09-18 19:00:31
你確定這段代碼是'matrix [row] = sc.nextLine()。toCharArray();'做了你想要的嗎?你想在這裏做什麼? – Pshemo 2014-09-18 19:00:41
@Pshemo yeap,我敢肯定 – underline 2014-09-18 19:01:44