大家好,我创建了一个数组的排序列表,但当我搜索字符w时,它没有找到。可能有什么问题。
public class BinarySearch {
public static void main(String[] args) {
char[] nam = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','X','W','Y','Z'};
char code;
int pos;
int bck = 0;
while (bck == 0) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter the Alphabet: ");
String str = sc.next();
code = str.toUpperCase().charAt(0);
pos = Arrays.binarySearch(nam, code);
if (pos >= 0) {
System.out.println("Alphabet "+code+" is at position "+ ++pos);
}else{
System.out.println("Character not found!");
}
}
}
1条答案
按热度按时间ktecyv1j1#
正如@eran在评论中所说,问题出在你的字母表上
'V','W','X'
.你也不需要
int bck = 0
,因为您可以使while循环像always循环一样,直到您像这样停止程序:while(true)
.