- ต่อข้อความด้วย
+ - ใช้
printfกำหนดจำนวนตำแหน่งทศนิยม - เปรียบเทียบเนื้อหา String ด้วย
.equals(...)
สร้างไฟล์ FactoryReport.java:
public class FactoryReport {
public static void main(String[] args) {
// เพิ่มโค้ดแต่ละส่วนตรงนี้
}
}String machineId = "M-001";
String machineName = "Mixer";
double temperature = 65.567;
int hours = 120;String title = "SMART FACTORY REPORT";
System.out.println("=== " + title + " ===");
System.out.println(machineId + " | " + machineName);System.out.printf("Temperature: %.1f C%n", temperature);
System.out.printf("Operating hours: %d%n", hours);%sใช้กับข้อความ%dใช้กับจำนวนเต็ม%.1fแสดงทศนิยมหนึ่งตำแหน่ง%nขึ้นบรรทัดใหม่
เมื่อเข้าใจแล้วจึงทดลองจัดเป็นตาราง:
System.out.printf("%-8s %-15s %10s %8s%n", "ID", "NAME", "TEMP", "HOURS");
System.out.printf("%-8s %-15s %8.1f C %8d%n", machineId, machineName, temperature, hours);String status = "RUNNING";
boolean running = status.equals("RUNNING");
System.out.println("Running: " + running);ใช้ .equals(...) เปรียบเทียบเนื้อหา String ไม่ใช้ ==
เพิ่มตัวแปร Location และ Status จากนั้นเพิ่มเป็นสองคอลัมน์ใหม่ในตาราง