int不能转换为字符串?

时间:2014-11-09 12:15:48

标签: java string integer

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

以下代码适用于我

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

但是,我正在尝试编写这些新指令时遇到问题。 我如何得到平方,转换为整数,加1然后再将值放回去?

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

4 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:0)

为什么int可以转换为String(除非存在隐式转换,但不是这样,Java编译器,原始包装器和字符串没有很多隐含的东西串联)?

使用以下两个选项之一:

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

答案 2 :(得分:0)

square.setText(x+"")

会工作

答案 3 :(得分:-1)

使用您自己的代码 String z = Integer.toString(x); square.setText(z);

相关问题