数组排序和搜索Java [英] Array Sorting and Searching Java
问题描述
我正在尝试从学生信息表中编写程序.该程序应该按字母顺序按学生姓名排序信息,然后为用户提供按姓名搜索学生的个人信息的选项.
I'm trying to do a program from a student info sheet. The program is supposed to order the info by student name alphabetically and then give the option for the user to search a student's individual info by name.
我用学生信息创建了一个数组,然后使用sort对其进行排序,并使用for循环将其输出,一切都很好.然后,我使用缓冲的阅读器请求用户输入,并进行了切换以缩小输入名称,然后使用if来查看是否在列表中.和往常一样,这是我的代码的一部分:
I created an array with the student info, then used sort to order it and a for loop to output it, all was good. Then i used the buffered reader to ask for user input, and a switch in order to narrow the input name, then an if to see if/else the searched name is in the list or not. As usual, here is part of my code:
System.out.println("Welcome to the student list program");
System.out.println("This is the list in alphabetical order:");
System.out.println("");
String[] list = new String[20];
list[0] = "Andrew ID: 8374 Age: 19 Grade: 88";
list[1] = "Ronald ID: 8953 Age: 17 Grade: 72";
list[2] = "Kate ID: 0071 Age: 15 Grade: 97";
list[3] = "Eve ID: 7348 Age: 17 Grade: 97";
list[4] = "Barney ID: 4704 Age: 15 Grade: 70";
list[5] = "James ID: 6259 Age: 20 Grade: 51";
list[6] = "Tiberius ID: 8090 Age: 18 Grade: 94";
list[7] = "George ID: 5059 Age: 18 Grade: 96";
list[8] = "William ID: 2057 Age: 20 Grade: 72";
list[9] = "Rose ID: 4977 Age: 17 Grade: 75";
list[10] = "Kylie ID: 4407 Age: 17 Grade: 85";
list[11] = "Mary ID: 9642 Age: 19 Grade: 62";
list[12] = "Joey ID: 3437 Age: 20 Grade: 54";
list[13] = "Ross ID: 5040 Age: 20 Grade: 64";
list[14] = "Chandler ID: 7931 Age: 18 Grade: 78";
list[15] = "Monica ID: 9238 Age: 19 Grade: 92";
list[16] = "Rachel ID: 0682 Age: 20 Grade: 99";
list[17] = "Phoebe ID: 3456 Age: 18 Grade: 64";
list[18] = "Bart ID: 0638 Age: 17 Grade: 73";
list[19] = "Lisa ID: 5233 Age: 16 Grade: 52";
boolean check = false;
Arrays.sort(list);
int a = 0;
while (a <= list.length) {
if (a == list.length){
check = true;
}
else {
System.out.println(list[a]);
a = a+1;
}
}
if (check) {
System.out.println("input name to search list");
System.out.println("");
InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader stdIn = new BufferedReader(inStream);
String input =stdIn.readLine();
input = input.toLowerCase();
char n = input.charAt(0);
switch (n){
case 'a':
if (input.equals("andrew")) {
System.out.println(list[0]);
}
else{
System.out.println("Input name is not in the list");
}
break;
问题在于程序将输出排序后的列表,但不会提供输入名称的选项.如您所见,我使用了布尔变量"check"来尝试控制程序流,但是它无法正常工作.我的意思是,现在,在输出排序后的列表之后,该程序将让我键入内容,但是首先没有输出输入名称到搜索列表",而且,在我键入一个名称并按Enter键之后,该程序将再次执行行来写,但不执行switch选项.
The problem is that the program will output the sorted list but it will not give the option to input a name. As you can see, I used the boolean variable "check" to try and control the program flow but it did not work correctly. By this I mean that now, after outputting the sorted list, the program will let me type, but without the output "input name to search list" coming first, also, after I type a name and hit enter the program will just do another line for me to write, but not execute the switch option.
推荐答案
Java从0到数字-1计数数组,因此循环永远不会终止.您需要将其更改为 a<仅list.length
.您还需要更改设置 check
的方式,因为使用固定循环永远都无法到达那里.如果(a == list.length-1),则必须为.
Java counts arrays from 0 to number - 1, so your loop never terminates. You need to change it to be a < list.length
only. You also need to change how you set check
, cause you'll never get there with the fixed loop; it needs to be if (a == list.length - 1)
.
但是,for循环会更容易且更具可读性:
However, a for loop would be much easier and more readable:
for (String s : list) {
System.err.println(s);
}
我不太明白为什么仍然需要一个检查变量,因为它总是被设置为true.
I don't quite understand why you need a check variable anyway, cause it will always be set to true.
这篇关于数组排序和搜索Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!