首页 > 代码库 > Java开发中经典的小实例-(打印输入重复的值)
Java开发中经典的小实例-(打印输入重复的值)
import java.util.ArrayList;
import java.util.Scanner;
public class Test8 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入:");
String number = input.next();
ArrayList<Character> arrayList = new ArrayList<Character>(); // 存放重复的数字;
char[] array = new char[String.valueOf(number).length()];
for (int i = 0; i < array.length; i++) {
array[i] = String.valueOf(number).charAt(i);
}
for (int i = 0; i < array.length; i++) {
for (int o = i + 1; o < array.length; o++) {
if (array[i] == array[o]) {
if (i == 0) {
arrayList.add(array[i]);
} else if (!isExist(arrayList, array[i])) {
arrayList.add(array[i]);
break;
}
}
}
}
for (int i = 0; i < arrayList.size(); i++) {
System.out.println("重复的数字是:" + arrayList.get(i));
}
}
public static boolean isExist(ArrayList<Character> arrayList, char num) {
for (int i = 0; i < arrayList.size(); i++) {
if (num == arrayList.get(i))
return true;
}
return false;
}
}
Java开发中经典的小实例-(打印输入重复的值)