我正在编写一个程序,该程序将以字符串形式输出唯一字符(通过扫描仪输入)。我创建了一个方法来尝试实现此目的,但我一直在获取不重复的字符,而不是字符串中唯一的一个(或多个)字符。我只想要唯一的字母。
这是我的代码:
import java.util.Scanner;
public class Sameness{
public static void main (String[]args){
Scanner kb = new Scanner (System.in);
String word = "";
System.out.println("Enter a word: ");
word = kb.nextLine();
uniqueCharacters(word);
}
public static void uniqueCharacters(String test){
String temp = "";
for (int i = 0; i < test.length(); i++){
if (temp.indexOf(test.charAt(i)) == - 1){
temp = temp + test.charAt(i);
}
}
System.out.println(temp + " ");
}
}
这是上面代码的示例输出:
Enter a word:
nreena
nrea
预期的输出将是: ra