首页 > 解决方案 > 我的“if-else”语句有问题,我不知道如何将我的文本字段转换为整数(或其他东西)

问题描述

我不知道如何将我的文本字段(txtGood 和 txtBad)转换为整数,所以我尝试了字符串,但没有用。我应该使用什么?

如您所见,尝试了“字符串”:

private void btnSubstituteActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:

    String o, p;
    o = txtGood.getText();
    p = txtBad.getText();

    if(o = p) {
        JOptionPane.showMessageDialog(null, "SampleQuoteGood", "Inspirational quote of the day:", JOptionPane.PLAIN_MESSAGE);
    }

我运行了程序,但我的按钮 (btnSubstitute) 不起作用。删除代码中的“if”和“String”是可行的。它显示了 JOptionpane 等等。

标签: javanetbeans

解决方案


解析String. _ 喜欢int one = Integer.parseInt("1");和比较==使用=的是赋值。所以,像

if (Integer.parseInt(o) == Integer.parseInt(p)) {

推荐阅读