System.out.println("请输入一个字符串");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();//获取的字符串
char[] arr = s.toCharArray();
System.out.println("请输入一个字符");
char cc = sc.nextLine().charAt(0);//字符
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if(arr[i]==cc){
list.add(i);
}
}
if(list.size()==0){
Random random = new Random();
int index = random.nextInt(s.length()-1);//随机的索引
Character c2 = s.charAt(index);//随机的字符
for (int i = 0; i < arr.length; i++) {
if(c2==arr[i]){
list.add(i);
}
}
System.out.println("字符不存在,随机得到字符: "+c2);
}