java如何輸出多個(gè)變量

小億
121
2024-09-03 21:39:38

在Java中,你可以使用System.out.println()方法來(lái)輸出多個(gè)變量。將變量用逗號(hào)分隔,或者將它們拼接成一個(gè)字符串。下面是兩種方法的示例:

方法1:使用逗號(hào)分隔變量

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:將變量拼接成一個(gè)字符串

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);
    }
}

這兩種方法都會(huì)在控制臺(tái)輸出以下內(nèi)容:

a = 10, b = 3.14, c = Hello

0