编写java程序:提示用户输入一个字符串,然后报告该提示是否是回文串?
1个回答
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestQuestion1 extends JFrame {
private JPanel jp;
private JTextField jtf;
private JButton jb;
public static void main(String[] args) {
new TestQuestion1();
}
public TestQuestion1() {
this.setSize(500, 100);
this.setLocation(100, 100);
this.setResizable(false);
String str = JOptionPane.showInputDialog(this, "请输入字符串");
boolean b = checkStr(str);
if(b) {
JOptionPane.showMessageDialog(jp, str+"是回文字符串");
} else {
JOptionPane.showMessageDialog(jp, str+"不是回文字符串");
}
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class BtnClick implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb) {
String str = jtf.getText();
boolean b = checkStr(str);
if(b) {
JOptionPane.showMessageDialog(jp, str+"是回文字符串");
} else {
JOptionPane.showMessageDialog(jp, str+"不是回文字符串");
}
}
}
}
public boolean checkStr(String str) {
StringBuffer sb = new StringBuffer(str);
sb = sb.reverse();
if(str.equals(sb.toString()))
return true;
return false;
}
}
呵呵,这个是今年Itat的初赛题吧
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestQuestion1 extends JFrame {
private JPanel jp;
private JTextField jtf;
private JButton jb;
public static void main(String[] args) {
new TestQuestion1();
}
public TestQuestion1() {
this.setSize(500, 100);
this.setLocation(100, 100);
this.setResizable(false);
String str = JOptionPane.showInputDialog(this, "请输入字符串");
boolean b = checkStr(str);
if(b) {
JOptionPane.showMessageDialog(jp, str+"是回文字符串");
} else {
JOptionPane.showMessageDialog(jp, str+"不是回文字符串");
}
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class BtnClick implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb) {
String str = jtf.getText();
boolean b = checkStr(str);
if(b) {
JOptionPane.showMessageDialog(jp, str+"是回文字符串");
} else {
JOptionPane.showMessageDialog(jp, str+"不是回文字符串");
}
}
}
}
public boolean checkStr(String str) {
StringBuffer sb = new StringBuffer(str);
sb = sb.reverse();
if(str.equals(sb.toString()))
return true;
return false;
}
}
呵呵,这个是今年Itat的初赛题吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询