温馨提示×

java println如何输出多个变量

小亿
105
2024-09-03 06:35:38
栏目: 编程语言

在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

0