下面的代碼:如何在此特定情況下將字符串轉換爲int?
if (e.getSource() == btnRe) {
lblCounter.setText(count - count);
}
拋出異常:
Exception: Incompatible tpyes: int cannot be converted to String "count - count"
我不知道怎麼改算成整數,該方法的setText可以讀。
下面的代碼:如何在此特定情況下將字符串轉換爲int?
if (e.getSource() == btnRe) {
lblCounter.setText(count - count);
}
拋出異常:
Exception: Incompatible tpyes: int cannot be converted to String "count - count"
我不知道怎麼改算成整數,該方法的setText可以讀。
count - count
是0
。你可以使用
lblCounter.setText(String.valueOf(count - count));
或只是
lblCounter.setText("0");
如果您連接""
方程,java會知道你傳遞一個字符串。 如果計數是String
,這應該會有訣竅。
if (e.getSource() == btnRe) {
lblCounter.setText("" + (Integer.parseInt(count) - Integer.parseInt(count)));
}
或者,如果它是一個int
,那就用這個。
if (e.getSource() == btnRe) {
lblCounter.setText("" + (count -count));
}
使用'String.valueOf()' – Jyr
使用'String#valueOf(count-count)'。但是,另一方面,'count - count'不是'0'?或者你想達到什麼目的? –
目標是從一個頁面到另一個頁面,中間有一個計數器,而不是一路返回到0我想製作一個按鈕,您只需按下並將您自動帶回0。 – Hooga