首页 > 解决方案 > 循环内的 printf

问题描述

我一直在玩 printf 方法,我对这段代码的输出感到困惑:

public static void main(String[] args) {
    String[] names = {"Sam, John, George"};
    double[] balances = {1000.97,100.00,87.00};
    for(int i=0; i<names.length; i++){
        System.out.printf("Hello %s      this is your balance %.2f\n",names[i],balances[i]);
    }

}
The output: Hello Sam, John, George      this is your balance 1000.97

我希望每个人在控制台中有三个打印语句。

感谢您的帮助。

标签: javaprintf

解决方案


"Sam, John, George"是一个字符串。

你错过了一些报价。它应该是:

String[] names = {"Sam", "John", "George"};

推荐阅读