在Java中,你可以使用System.out.println()
方法來輸出多個變量。將變量用逗號分隔,或者將它們拼接成一個字符串。下面是兩種方法的示例:
方法1:使用逗號分隔變量
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);
}
}
方法2:將變量拼接成一個字符串
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