繁体   English   中英

在这种特定情况下如何将字符串转换为int?

[英]How to convert string to int in this specific situation?

以下代码:

if (e.getSource() == btnRe) {
  lblCounter.setText(count - count);
}

引发异常:

Exception: Incompatible tpyes: int cannot be converted to String "count - count"

我不知道如何将计数更改为setText方法可以读取的整数。

count - count0 你可以用

lblCounter.setText(String.valueOf(count - count));

要不就

lblCounter.setText("0");

如果将""连接到方程式,java将知道您正在传递字符串。 如果count是一个String ,这应该可以解决问题。

    if (e.getSource() == btnRe) {

        lblCounter.setText("" + (Integer.parseInt(count) - Integer.parseInt(count)));
    }

或者,如果它是一个int ,则使用它。

    if (e.getSource() == btnRe) {

        lblCounter.setText("" + (count -count));
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM