java int 不能转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26828036/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:42:45  来源:igfitidea点击:

int cannot be converted to string?

javastringinteger

提问by Vestel

I am trying to write the instructions to notice when circle is about to be 12 and then add 1 to square.

我正在尝试编写说明以注意圆何时将变为 12,然后将 1 添加到正方形。

The code below works fine for me

下面的代码对我来说很好用

   int x = Integer.parseInt(circle.getText()); 
   x = x + 1;
   String z = Integer.toString(x);
   circle.setText(z);

However, I am having trouble with these new instructions I am trying to write. How can I get square, convert to integer, add 1 and then put the value back?

但是,我在尝试编写这些新指令时遇到了麻烦。我怎样才能得到平方,转换为整数,加1然后把值放回去?

   int q = Integer.parseInt(square.getText());
   x = q + 1;
   square.setText(x);

回答by Eran

You can convert to Stringusing Integer.toString():

您可以转换为String使用Integer.toString()

square.setText(Integer.toString(x));

回答by Dici

Why would an intbe convertible to a String(unless an implicit conversion exists, which is not the case, there is not a lot of implicit things with the Java compiler, primitive wrappers and string concatenation apart) ?

为什么 aint可以转换为 a String(除非存在隐式转换,事实并非如此,Java 编译器、原始包装器和字符串连接并没有很多隐式的东西)?

Use one of these two options :

使用以下两个选项之一:

square.setText(String.valueOf(x));
square.setText(x + "");

回答by Don Chakkappan

square.setText(x+"")

Will work

将工作