简体   繁体   English

在2D数组中搜索关键字

[英]Searching keyword in a 2D array

I'm trying to make a small method that will allow a user to input a crime into a "criminal database" and it will show only the criminals that committed that crime. 我试图制作一个小方法,允许用户将犯罪输入“犯罪数据库”,它只会显示犯下该罪行的罪犯。

Unfortunately, I have to use a 2D array. 不幸的是,我必须使用2D数组。 I've written a method, but it's not giving me the output I'm looking for. 我写了一个方法,但它没有给我我正在寻找的输出。

Here's the method: 这是方法:

    //method to search data
public static void searchData() throws IOException{

    int flag = 0;
    boolean found = false;

//input search key
    System.out.print("Which crime would you like to select? (arson, theft, assault) ");
    String searchKey = br.readLine();

    System.out.println("You searched for criminals with the offence of \"" + searchKey + "\".");    
    System.out.println("Name - Crime - Year of Conviction");

    for(int i = 0; i < criminals.length; i++){
        if(searchKey.compareTo(criminals[i][1]) == 0){
            flag = i;
            found = true;
        }
    }

    if(found == false){
        System.out.println("Error! Crime not found.");
    }else{
        System.out.println("Criminals found.");
        for(int i = 0; i < criminals.length; i++){
            System.out.println(criminals[flag][0] + " - " + criminals[flag][1] + " - " + criminals[flag][2]);
        }
    }
} 

My input was this: 我的意见如下:

George - theft - 1999
Eddie - assault - 2003
Al - theft - 1999

And here is the output after testing: 这是测试后的输出:

Which crime would you like to select? (arson, theft, assault) theft
You searched for criminals with the offence of "theft".
Criminals found.
Al - theft - 1999
Al - theft - 1999
Al - theft - 1999

Could you help me figure out what's wrong with this? 你能帮我弄清楚这有什么问题吗? Thanks in advance. 提前致谢。 :) :)

You have been printing last found criminal (flag). 你一直在打印最后发现的罪犯(国旗)。

for(int i = 0; i < criminals.length; i++){
    if(searchKey.compareTo(criminals[i][1]) == 0){
        if(!found) {
            found = true;
            System.out.println("Criminals found.");
        }
        System.out.println(criminals[i][0] + " - " + criminals[i][1] + " - " + criminals[i][2]);
    }
}

if(found == false){
    System.out.println("Error! Crime not found.");
}

Just some small additions to enhance code reuse and maintainability (if desired): 只是一些小的补充,以增强代码重用和可维护性(如果需要):

  • you've already understood that using arrays is not the best (= most "readable") solution in many real-life cases, since a symbol (natural language) is usually better understood than a plain number - EDIT: in this case using constants may already be better than literal numbers 你已经明白在许多现实生活中使用数组并不是最好的(=最“可读”)解决方案,因为符号(自然语言)通常比普通数字更好理解 - 编辑:在这种情况下使用常量可能已经比文字数字更好了
  • use the enhanced for loop instead of explicit element addressing whenever possible: What is the syntax of enhanced for loop in Java? 尽可能使用增强型for循环而不是显式元素寻址: Java中增强for循环的语法是什么?
  • instead of comparing to false you may want to use the ! 而不是比较false你可能想要使用! operator to make your code more readable 运算符使您的代码更具可读性
  • also, .equals is clearer than .compareTo .. == 0 (at least in this case) 同样, .equals.compareTo .. == 0更清晰(至少在这种情况下)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM