我打印0-99的值和食物數組中的一個隨機字符串。我似乎無法正確輸出它們到表格。初學java - 打印表格
String foods[] = {"Bread", "Pizza", "Cheese"};
for (int x = 0; x<=99; x++) {
for (int i = 0; i<4; i++) {
int random = (int) (Math.random() * 3);
System.out.print(x + " - " + foods[random] + "\t\t");
}
System.out.println();
}
實際輸出:
0 - Pizza 0 - Bread 0 - Cheese 0 - Bread
1 - Bread 1 - Pizza 1 - Bread 1 - Pizza
.... until 99
預期輸出:
0 - Pizza 1 - Bread 2 - Cheese 3 - Bread
4 - Bread 5 - Pizza 6 - Bread 7 - Pizza
.... until 99
是什麼你要找的則輸出? – Rogue
在另一個註釋中:嘗試在所有代碼中保持一致。在這種情況下,在for語句中,始終使用'<'或'<='但不要混淆,這會在將來幫助您,在執行更復雜的代碼時提高可讀性。 –