public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String[] rows = new String[20];
int num_rows = 0;
while (keyboard.hasNextLine())
{
rows[num_rows] = keyboard.nextLine();
num_rows++;
}
int width = rows[0].length();
for(int c = 0; c < width; c++){
for(int r = 0; r < num_rows; r++)
System.out.print(rows[r].charAt(c));
System.out.println();
}
}
我使用NetBeans和MAC停止編程。我試圖用Ctrl或Cmd + d或z來結束輸入。 Ctrl + d結束輸入,但此代碼:結束輸入精加工而成
int width = rows[0].length();
for(int c = 0; c < width; c++){
for(int r = 0; r < num_rows; r++)
System.out.print(rows[r].charAt(c));
System.out.println();
}
不會運行。爲什麼它會停止輸入但不運行代碼的其餘部分?
UPDATE:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int num_rows = 0;
while (keyboard.hasNextLine())
{
num_rows++;
}
System.out.println("");
}
您如何知道代碼無法運行?我運行該程序並確實執行了代碼,但引發了異常。 – Sweeper
[什麼是調試器,它如何幫助我診斷問題?](http://stackoverflow.com/q/25385173/5221149) – Andreas
對於我們來說,如果您可以通過此指定要實現的目標,那將很容易程序。 – Maverick