printf在for循环内部

huangapple go评论81阅读模式
英文:

printf inside for loop

问题

I have been playing around with the printf method and I am confused with the output from this code:

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

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:

  1. public static void main(String[] args) {
  2. String[] names = {&quot;Sam, John, George&quot;};
  3. double[] balances = {1000.97,100.00,87.00};
  4. for(int i=0; i&lt;names.length; i++){
  5. System.out.printf(&quot;Hello %s this is your balance %.2f\n&quot;,names[i],balances[i]);
  6. }
  7. }
  8. 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"};

英文:

&quot;Sam, John, George&quot; is one string.

You're missing some quotes. It should be:

  1. String[] names = {&quot;Sam&quot;, &quot;John&quot;, &quot;George&quot;};

huangapple
  • 本文由 发表于 2020年8月6日 05:35:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63273859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定