英文:
printf inside for loop
问题
I have been playing around with the printf method and I am confused with the output from this code:
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
I expected three print statements in the console for each person.
Thank for any help.
英文:
I have been playing around with the printf method and I am confused with the output from this code:
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
I expected three print statements in the console for each person.
Thank for any help.
答案1
得分: 5
"Sam, John, George" is one string.
You're missing some quotes. It should be:
String[] names = {"Sam", "John", "George"};
英文:
"Sam, John, George"
is one string.
You're missing some quotes. It should be:
String[] names = {"Sam", "John", "George"};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论