所以,问题来了。我想在文本中输入数字,所以它显示为“Number: 18”。但是,对于每个用户来说,数字都会不同,这取决于用户的答案。我应该使用什么函数来做呢?我以前一直在 pawno 工作过(游戏),就像
format(字符串, sizeof(字符串), "数字 %d", 数字);
我尝试过这个,但似乎不起作用。文本就变成空白了。 这是我的完整代码:
TextView tv6 = (TextView)findViewById(R.id.textView3);
String string = "";
format(string,256, "Your chances are %d % !",rezultat);
tv6.setText(string);
最佳答案
试试这个:
String string = "Your chances are " + rezultat + " % !";
为什么你的代码不起作用?
format(string,256, "Your chances are %d % !",rezultat);
format()
将返回格式化的字符串,并且不会格式化字符串本身。 (String
是不可变的,还记得吗?)因此,您需要将其返回值存储在字符串中,然后使用它。像这样的东西:
A.
String str = String.format(...);
tv6.setText(str);
B.
string = String.format(...);
tv6.setText(string);
C.
tv6.setText(String.format(...));
要以 %
格式打印,您需要像 %%
那样编写。
关于java - 数字变量到字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25490979/