import javax.swing.*; import java.awt.*; import java.util.stream.IntStream; public class PalindromeChecker extends JFrame { private JTextField inputField, outputField; private JButton recursiveButton, nonRecursiveButton; public PalindromeChecker() { setTitle("Palindrome Checker"); setSize(300, 150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(3, 2)); panel.add(new JLabel("Input:")); inputField = new JTextField(); panel.add(inputField); panel.add(new JLabel("Output:")); outputField = new JTextField(); outputField.setEditable(false); panel.add(outputField); recursiveButton = new JButton("Check Recursively"); recursiveButton.addActionListener(e -> checkPalindrome(true)); panel.add(recursiveButton); nonRecursiveButton = new JButton("Check Non-Recursively"); nonRecursiveButton.addActionListener(e -> checkPalindrome(false)); panel.add(nonRecursiveButton); setContentPane(panel); setVisible(true); } private void checkPalindrome(boolean isRecursive) { String input = inputField.getText(); if (isPalindrome(input, isRecursive)) { outputField.setText("It is a palindrome."); } else { outputField.setText("It is not a palindrome."); } } private boolean isPalindrome(String input, boolean isRecursive) { if (isRecursive) { return isPalindromeRecursive(input); } else { return isPalindromeNonRecursive(input); } } private boolean isPalindromeRecursive(String input) { if (input.length() <= 1) { return true; } else if (input.charAt(0) == input.charAt(input.length() - 1)) { return isPalindromeRecursive(input.substring(1, input.length() - 1)); } else { return false; } } private boolean isPalindromeNonRecursive(String input) { int length = input.length(); return IntStream.range(0, length / 2) .noneMatch(i -> input.charAt(i) != input.charAt(length - i - 1)); } public static void main(String[] args) { new PalindromeChecker(); } }给上述代码标注注释
时间: 2023-06-01 13:06:34 浏览: 158
java拼图小游戏项目开发教程.docx
//导入 java.swing 包和 java.awt 包以及 java.util.stream.IntStream;
import javax.swing.*;
import java.awt.*;
import java.util.stream.IntStream;
//创建一个 PalindromeChecker 类,继承自 JFrame 类
public class PalindromeChecker extends JFrame {
//创建输入框、输出框和两个按钮的成员变量
private JTextField inputField, outputField;
private JButton recursiveButton, nonRecursiveButton;
//构造函数,设置窗口标题、大小、关闭操作和布局
public PalindromeChecker() {
setTitle("Palindrome Checker");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建一个面板,采用 3 行 2 列的格子布局,并添加标签、输入框、输出框和两个按钮
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(new JLabel("Input:"));
inputField = new JTextField();
panel.add(inputField);
panel.add(new JLabel("Output:"));
outputField = new JTextField();
outputField.setEditable(false);
panel.add(outputField);
recursiveButton = new JButton("Check Recursively");
recursiveButton.addActionListener(e -> checkPalindrome(true)); //添加递归按钮的事件监听器
panel.add(recursiveButton);
nonRecursiveButton = new JButton("Check Non-Recursively");
nonRecursiveButton.addActionListener(e -> checkPalindrome(false)); //添加非递归按钮的事件监听器
panel.add(nonRecursiveButton);
//将面板设置为窗口内容面板,并设置窗口可见
setContentPane(panel);
setVisible(true);
}
//检查回文字符串的方法,根据参数决定使用递归或者非递归方式
private void checkPalindrome(boolean isRecursive) {
String input = inputField.getText(); //获取输入框中的字符串
if (isPalindrome(input, isRecursive)) { //判断是否为回文字符串
outputField.setText("It is a palindrome."); //如果是,输出 "It is a palindrome."
} else {
outputField.setText("It is not a palindrome."); //如果不是,输出 "It is not a palindrome."
}
}
//判断字符串是否为回文字符串的方法,根据参数决定使用递归或者非递归方式
private boolean isPalindrome(String input, boolean isRecursive) {
if (isRecursive) { //如果使用递归方式
return isPalindromeRecursive(input); //调用递归方法
} else { //如果使用非递归方式
return isPalindromeNonRecursive(input); //调用非递归方法
}
}
//递归方式判断字符串是否为回文字符串的方法
private boolean isPalindromeRecursive(String input) {
if (input.length() <= 1) { //字符串长度小于等于 1 时,返回 true
return true;
} else if (input.charAt(0) == input.charAt(input.length() - 1)) { //如果首尾字符相同
return isPalindromeRecursive(input.substring(1, input.length() - 1)); //递归调用本方法,传入去除首尾字符后的子串
} else {
return false;
}
}
//非递归方式判断字符串是否为回文字符串的方法
private boolean isPalindromeNonRecursive(String input) {
int length = input.length(); //获取字符串长度
return IntStream.range(0, length / 2) //对于从 0 到 length/2 的每个整数 i,执行如下操作
.noneMatch(i -> input.charAt(i) != input.charAt(length - i - 1)); //如果存在不等的字符,返回 false;否则返回 true
}
//主函数,创建一个 PalindromeChecker 对象
public static void main(String[] args) {
new PalindromeChecker();
}
}
阅读全文