如何将 int 放入字符串中 (java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5329556/
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
How do I put an int in a string (java)
提问by Jessica
Here's my problem:
这是我的问题:
public class Sum {
private int sum;
private String sequence = " ";
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public String getSequence() {
return sequence;
}
public void setSequence(int sum) {
this.sequence = sequence + " " + sum;
}
}
And the types are incompatible in line 15, which is "required: int"
并且类型在第 15 行中不兼容,这是“必需的:int”
Can anybody help me with this? Please and thank you.
有人可以帮我解决这个问题吗?谢谢,麻烦您了。
*editSorry for not being able to put line 15. Line 15 is:
*编辑很抱歉无法放置第 15 行。第 15 行是:
return sequence;
回答by Piyush Mattoo
Not sure exactly what you are asking for but you can convert int to String via String sequence= Integer.toString(sum);
and String to int via int sum = Integer.parseInt(sequence.trim());
不确定您要什么,但您可以将 int 转换为 String viaString sequence= Integer.toString(sum);
并将 String转换为 int viaint sum = Integer.parseInt(sequence.trim());
回答by Piyush Mattoo
The java code actually works pretty well for me, I'm compiling it with Java 1.6.0_24 on a Mac. Have you tried to implement a main to do some debug?
Java 代码实际上对我来说效果很好,我在 Mac 上用 Java 1.6.0_24 编译它。你有没有试过实现一个 main 来做一些调试?
public static void main(String args[]){
Sum s = new Sum();
s.setSequence(3);
s.setSequence(9);
System.out.println(s.getSequence());
}