Java 程式將整數和字串連線


要將整數與字串值連線起來,需要使用 + 運算子。

假設字串如下。

String str = "Demo Text";

現在,我們將整數值連線到上述字串。

String res = str + 1 + 2 + 3 + 4 + 5;

示例

 線上演示

public class Demo {
    public static void main(String[] args) {
       String str = "Demo Text";
       System.out.println("String = "+str);
       String res = 99 + 199 + 299 + 399 + str;
       System.out.println(res);
    }
}

輸出

String = Demo Text
996Demo Text

更新時間:2020-06-26

468 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

開始
廣告