← 從零開始學 Java
第 2 課,共 88 課 · 10 XP
輸出
理論與題目說明
機器翻譯,尚未經母語人士審閱。
System.out.println(x)——輸出後換到下一行;System.out.print(x)——不換行;System.out.printf(format, ...)——像 C 一樣的格式化輸出。
System.out.print("Score: ");
System.out.println(2 + 3); // Score: 5
System.out.println("2 + 3 = " + 2 + 3); // 2 + 3 = 23——字串 + 數字從左邊開始串接!
System.out.println("2 + 3 = " + (2 + 3)); // 2 + 3 = 5
System.out.printf("%s: %d points, %.1f%%%n", "Kai", 42, 97.5); // Kai: 42 points, 97.5%
%d——整數、%s——字串、%.2f——小數點後 2 位的實數、%n——換行、%%——百分比符號本身。
字串裡:\n 是換行、\t 是定位字元、\" 是引號。
題目。 輸出三行:
IGD Games
2 + 3 = 5
"Java"
5 要用運算式求出,不是寫成文字。