java - 循环内的 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
我希望每个人在控制台中有三个打印语句。
感谢您的帮助。
解决方案
"Sam, John, George"
是一个字符串。
你错过了一些报价。它应该是:
String[] names = {"Sam", "John", "George"};
推荐阅读
- java - 具有 OutOfMemoryError 的 Java Docker 容器
- python - gmaps无法在vscode(python)中显示图形
- android - 添加 Kapt 插件后 - 执行 org.jetbrains.kotlin.gradle.internal.KaptExecution 时发生故障
- javascript - 如何在 v-Calendar 中格式化日期?
- javascript - 出现三个菜单图标而不是一个。(出现两个不成文的菜单图标 div)
- ruby-on-rails - Minimagick 不减少 png 文件大小而是增加
- php - Laravel - 获取根路径'/'以外的cookie
- networking - 在通过网络发送浮点数之前对浮点数进行位移的原因是什么
- npm - 维护两个主要版本的 NPM 包
- sql - 如何使用 HQL 替换 Hive 数据库中的行