Java 数组中存在重复值,未找到重复值
我有一个程序,如果我输入一个数字,程序将跟踪重复的值,并且工作正常。它跟踪值是否存在任何重复,并显示重复的值 如果我输入了一个没有重复的数字,我将如何通知用户数组中没有重复的值 这是代码Java 数组中存在重复值,未找到重复值,java,arrays,Java,Arrays,我有一个程序,如果我输入一个数字,程序将跟踪重复的值,并且工作正常。它跟踪值是否存在任何重复,并显示重复的值 如果我输入了一个没有重复的数字,我将如何通知用户数组中没有重复的值 这是代码 case "Search Duplicate Value": { search = (String) JOptionPane.showInputDialog(null , "Search Duplicate Value" , "Search" , JOptionPane.INFORMATION_M
case "Search Duplicate Value":
{
search = (String) JOptionPane.showInputDialog(null , "Search Duplicate Value" , "Search" , JOptionPane.INFORMATION_MESSAGE);
int parseSearchDuplicate = Integer.parseInt(search);
for(int i = 0; i <= arrayElements.length ; i++)
{
if(arrayElements[i] == parseSearchDuplicate && i == parseSearchDuplicate)
{
equalisStringDuplicate = equalisStringDuplicate + " " + arrayElements[i];
}
}
}
JOptionPane.showMessageDialog(null , new Label(equalisStringDuplicate + " "), "Duplicate Value is:" , JOptionPane.INFORMATION_MESSAGE);
equalisStringDuplicate = "";
break;
案例“搜索重复值”:
{
search=(字符串)JOptionPane.showInputDialog(null,“搜索重复值”,“搜索”,JOptionPane.INFORMATION\u消息);
int parseSearchDuplicate=Integer.parseInt(搜索);
对于(int i=0;i使用布尔值,如果存在匹配项,则将其设置为true,并根据值向用户显示消息。请尝试下面的操作
case "Search Duplicate Value":
{
search = (String) JOptionPane.showInputDialog(null , "Search Duplicate Value" , "Search" , JOptionPane.INFORMATION_MESSAGE);
int parseSearchDuplicate = Integer.parseInt(search);
boolean isduplicate=false;
for(int i = 0; i < arrayElements.length ; i++)
{
if(arrayElements[i] == parseSearchDuplicate && i == parseSearchDuplicate)
{
isduplicate=true;
equalisStringDuplicate = equalisStringDuplicate + " " + arrayElements[i];
}
}
}
If(isduplicate)
JOptionPane.showMessageDialog(null , new Label(equalisStringDuplicate + " "), "Duplicate Value is:" , JOptionPane.INFORMATION_MESSAGE);
else
//show no duplicate found
equalisStringDuplicate = "";
break;
案例“搜索重复值”:
{
search=(字符串)JOptionPane.showInputDialog(null,“搜索重复值”,“搜索”,JOptionPane.INFORMATION\u消息);
int parseSearchDuplicate=Integer.parseInt(搜索);
布尔值isduplicate=false;
for(int i=0;i
等待出现问题我添加了一个try-catch,这样它就会知道输入是IndexOutOfBoundsException,而现在每当我开始搜索重复值时,这种情况实际上正在发生。您的循环导致问题,它应该运行到i