
本文旨在解决在Java程序中使用JOptionPane显示重复客户编号时,当存在多个重复项时无法正确显示的问题。我们将提供一种更清晰、更有效的方法来查找数组中的重复项,并将它们格式化为易于阅读的消息,最后通过JOptionPane显示出来。
原始代码在处理多个重复项时存在一些问题,导致JOptionPane无法正确显示所有重复的客户编号。为了解决这个问题,我们将采用一种更结构化的方法,将代码分解为更小的、更易于管理的部分。
1. 查找重复项的函数
首先,创建一个函数来查找数组中的重复项。该函数将接受一个整数数组作为输入,并返回一个包含所有重复项的列表。为了提高效率,可以使用 HashSet 来跟踪已经遇到的元素,以及 ArrayList 来存储重复项。
立即学习“Java免费学习笔记(深入)”;
import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class DuplicateFinder {
public static List<Integer> findDuplicates(int[] arr) {
Set<Integer> seen = new HashSet<>();
List<Integer> duplicates = new ArrayList<>();
Set<Integer> added = new HashSet<>(); // 防止重复添加相同的重复项
for (int num : arr) {
if (!seen.add(num) && added.add(num)) { // 如果添加失败,说明已经存在,是重复的
duplicates.add(num);
}
}
return duplicates;
}2. 格式化输出字符串的函数
接下来,创建一个函数来格式化重复项的列表,以便在JOptionPane中显示。该函数将接受重复项的列表作为输入,并返回一个格式化的字符串。
public static String formatDuplicates(List<Integer> duplicates) {
if (duplicates.isEmpty()) {
return "\nHONEST CUSTOMERS";
} else {
StringBuilder sb = new StringBuilder("Duplicates: ");
for (int i = 0; i < duplicates.size(); i++) {
sb.append("Customer #").append(duplicates.get(i));
if (i < duplicates.size() - 1) {
sb.append(", ");
}
}
return sb.toString();
}
}3. 显示消息框
最后,使用JOptionPane显示格式化的字符串。
public static void main(String[] args) {
int number;
number = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of customers: "));
int[] customerNumbers = new int[number];
for (int i = 0; i < number; i++) {
customerNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Customer number: "));
}
List<Integer> duplicates = findDuplicates(customerNumbers);
String message = formatDuplicates(duplicates);
JOptionPane.showMessageDialog(null, message);
}
}以下是完整的代码示例,包含了上述所有函数:
import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class DuplicateFinder {
public static List<Integer> findDuplicates(int[] arr) {
Set<Integer> seen = new HashSet<>();
List<Integer> duplicates = new ArrayList<>();
Set<Integer> added = new HashSet<>(); // 防止重复添加相同的重复项
for (int num : arr) {
if (!seen.add(num) && added.add(num)) { // 如果添加失败,说明已经存在,是重复的
duplicates.add(num);
}
}
return duplicates;
}
public static String formatDuplicates(List<Integer> duplicates) {
if (duplicates.isEmpty()) {
return "\nHONEST CUSTOMERS";
} else {
StringBuilder sb = new StringBuilder("Duplicates: ");
for (int i = 0; i < duplicates.size(); i++) {
sb.append("Customer #").append(duplicates.get(i));
if (i < duplicates.size() - 1) {
sb.append(", ");
}
}
return sb.toString();
}
}
public static void main(String[] args) {
int number;
number = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of customers: "));
int[] customerNumbers = new int[number];
for (int i = 0; i < number; i++) {
customerNumbers[i] = Integer.parseInt(JOptionPane.showInputDialog("Customer number: "));
}
List<Integer> duplicates = findDuplicates(customerNumbers);
String message = formatDuplicates(duplicates);
JOptionPane.showMessageDialog(null, message);
}
}通过将代码分解为更小的、更易于管理的部分,我们可以更清晰地查找和显示重复的客户编号。使用HashSet可以有效地检测重复项,而使用StringBuilder可以方便地格式化输出字符串。这种方法不仅解决了原始代码中的问题,还提高了代码的可读性和可维护性。 记住,良好的代码结构和清晰的逻辑是编写高质量程序的关键。
以上就是Java中查找并显示重复元素:优化JOptionPane输出的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号