在Java中,你可以使用System.out.println()
方法來輸出多個變量。將變量用逗號分隔,并放在括號內。例如:
public class Main {
public static void main(String[] args) {
int a = 10;
double b = 3.14;
String c = "Hello";
System.out.println("a = " + a + ", b = " + b + ", c = " + c);
}
}
這段代碼會輸出:
a = 10, b = 3.14, c = Hello
你也可以使用printf()
方法來格式化輸出。例如:
public class Main {
public static void main(String[] args) {
int a = 10;
double b = 3.14;
String c = "Hello";
System.out.printf("a = %d, b = %.2f, c = %s%n", a, b, c);
}
}
這段代碼會輸出:
a = 10, b = 3.14, c = Hello