有个实例,是在输入框里输入任意字符,然后判断末尾是否有?或者!。
请教如何调用JOptionPane.showInputDialog里输入框来用方法实现判断。
谢谢

解决方案 »

  1.   

    int index =indexOf("?")
    看看index跟你输入字符串长度相比较
      

  2.   

    当然也可以用正则表达式了。
    import javax.swing.*;public class JudgeChar extends JFrame {
    private String str;

    public JudgeChar(){
    str = JOptionPane.showInputDialog("Please input a String: ");

    if (str.charAt(str.length()-1) == '?' || str.charAt(str.length()-1) == '!')
    JOptionPane.showMessageDialog(null, "The String you input includes a \"?\" or \"!\" ended");
    else
    JOptionPane.showMessageDialog(null, "The String you input doesn't include a \"?\" or \"!\" ended");
    }

    public static void main(String args[]){
    JudgeChar app = new JudgeChar();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
      

  3.   

    最原始的正则。
    import javax.swing.*;public class JudgeChar extends JFrame {
    private String str;

    public JudgeChar(){
    str = JOptionPane.showInputDialog("Please input a String: ");

    if (str.matches("\\w*(\\?|\\!)"))
    JOptionPane.showMessageDialog(null, "The String you input ends with a \"?\" or \"!\"");
    else
    JOptionPane.showMessageDialog(null, "The String you input doesn't end with a \"?\" or \"!\" ended");
    }

    public static void main(String args[]){
    JudgeChar app = new JudgeChar();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
      

  4.   

    直接用String里的endWith("?")来判断不就OK了????
    例如:
    String str = "aasdf?"
    if (str.endWith("?") {...}