我需要找到从用户输入派生的 char 值的位置。但是当我不提供要查找的确切字符时, indexOf 方法似乎不起作用。但是由于 char 值是从用户的输入中得出的,所以我无法输入确切的值。有什么方法可以按我需要的方式使用 indexOf 吗?
public class pg2a {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String host;
System.out
.println("Please enter your sequence (any length, any characters):");
host = keyboard.nextLine();
System.out.println("Now enter a 3 character sequence:");
String candidate;
candidate = keyboard.nextLine();
int length = candidate.length();
char a = candidate.charAt(0);
char b = candidate.charAt(1);
char c = candidate.charAt(2);
int i = candidate.indexOf(a);
int j = candidate.indexOf(b);
int k = candidate.indexOf(c);
if (length == 3) {
if (i == -1)
System.out
.println("The 3 character sequence you entered is not a subsequence of your sequence.");
else
System.out.println("Let's go!");
} else {
System.out
.println("The sequence you entered is not a 3 character sequence.");
}
}
}
`