Java:system.out.println 在字符串中连接某些内容(非常简单的问题)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2130640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: system.out.println concatenating something within a string (really easy question)
提问by lemon
I want my output to be Candy[1]. counterInt = 1
我希望我的输出是 Candy[1]。counterInt = 1
But my compiler isn't taking this code:
但我的编译器没有采用以下代码:
System.out.println("Candy["+counterInt"]");
What can I do to have this variable appear within the string Candy[]?
我该怎么做才能让这个变量出现在字符串 Candy[] 中?
Candy[] is not an array, it's a string.
Candy[] 不是数组,而是字符串。
采纳答案by True Soft
You must put a +
after counterInt
:
你必须在+
之后放一个counterInt
:
System.out.println("Candy["+counterInt+"]");
回答by GuruKulki
you should have System.out.println("Candy[" + counterInt + "].counterInt = " + counterInt); to get Candy[1]. counterInt = 1;
你应该有 System.out.println("Candy[" + counterInt + "].counterInt = " + counterInt); 得到糖果[1]。counterInt = 1;
回答by phisch
You are missing a + after CounterInt.
您在 CounterInt 之后缺少一个 +。
回答by JRL
System.out.println("Candy["+counterInt+"]");
回答by sud03r
System.out.println("Candy["+counterInt+"].counterInt="+counterInt);