2017-06-30 80 views
0

有沒有更簡單的方法來做到這一點?簡單的方法來處理字符串數字?

int score = Integer.parseInt(tv_score.getText()); 
score += 100; 
tv_score.setText(String.valueOf(score)); 
+0

你可以做''「+分數' – QBrute

+2

將分數保留在類字段中,並且不要從TextView中獲得分數。 –

+0

java強制類型,你得和它一起生活:( – nafas

回答

0

是的,有你的原始對象數據類型的更簡單的方法,而不是原始數據類型

Integer score = new Integer(tv_score.getText()); 
score += 100; 
tv_score.setText(score.toString()); 

,或者您也可以像這樣,使之更短,精確

Integer score = new Integer(tv_score.getText())+100; 
tv_score.setText(score.toString()); 
+1

不要使用'new Integer()'。 'Integer.valueOf()'。 – QBrute

0

int score = Integer.parseInt(tv_score.getText());

如果在文本字段或textarea中輸入的數字不是十進制格式,這將不起作用。

If the number will always be in decimal format, then use 
- int x = Integer.parseInt("11") + 10; 

And for an instance, if the number is in hexadecimal format, then use 
- int x = Integer.parseInt("1B", 16) + 10; 

希望這會有所幫助。 :-)

相關問題