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); } }
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); } }
直接用String里的endWith("?")来判断不就OK了???? 例如: String str = "aasdf?" if (str.endWith("?") {...}
看看index跟你输入字符串长度相比较
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);
}
}
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);
}
}
例如:
String str = "aasdf?"
if (str.endWith("?") {...}