我想創建一個程序,允許用戶在輸入數組的行和列,輸入數組內的值和輸出數組之間進行選擇。它一切正常,直到我嘗試輸出數組,它總是輸出0。如何正確打印值?如何將值存儲在數組中?
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char ans='y';
int column=0, row=0;
do{
char c = menu(sc);
int array[][] = new int [row] [column];
switch (Character.toLowerCase(c))
{
case 'a': System.out.print("Enter row size ");
row=sc.nextInt();
System.out.print("Enter column size ");
column=sc.nextInt();
System.out.print("Row and Column "+row+" "+column);
break;
case 'b': for(int r=0;r<row;r++)
{
for(int col=0;col<column;col++)
{
System.out.print("Enter value for row "+r+" column "+col+": ");
array[r][col]=sc.nextInt();
}
}
break;
case 'c': for(int r=0; r<array.length; r++)
{
for(int col=0; col<array[r].length; col++)
{
System.out.print(array[r][col] + " ");
}
System.out.println();
}
break;
}
System.out.println("");
}while(ans=='y');
}
你檢查過矩陣的大小嗎?你不是一次又一次地替換同一個單元嗎? – Bharadwaj
什麼是菜單(sc)? –