有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java LOOK else if语句:我把它放在哪里

我不知道把ELSE语句放在哪里,如果我把它放在for循环中,在if语句之后,它会在ELSE语句中重复多次。但是如果我在ELSE语句中输入我想要输出的内容,例如“对不起,这个车牌找不到”在FOR循环之外,那么它会在我不想要的时候输出

    else // or 
            {
                // this code reads a file in
                String full="";
                try { FileReader reader = new FileReader("address.txt"); // new file reader to read in a file called address
                BufferedReader OwnerDetails = new BufferedReader(reader); // reads in the file efficiently

                String line; // assigning a string
                while ((line = OwnerDetails.readLine()) != null) { // reads in all file
                    full=full+line;

                }
                reader.close(); // the reader close, it finishes reading it in
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //System.out.println(full); // reads in whole file, and outputs it, used to check if the file had been written in
                    // it is commented out once program is finished, but is used when testing
                String[] details = full.split(","); // splits up info into arrays

                String searchword=registration; // searchword is set as the registration plate entered
                for (int i=0;i<details.length-2;i=i+1) 
                {
                    if(searchword.equals(details[i])) // if the search word is in the file
                    {
                        System.out.println("Name: " +details[i-1]); // outputs name
                        System.out.println("Registration: "+details[i]); // outputs reg plate again 
                        System.out.println("Address Line 1: "+details[i+1]); // outputs address
                        System.out.println("Address Line 2: "+details[i+2]); // outputs address line 2

                        fw2.write(details[i-1]+","); // separates the details when writing in to the file
                        fw2.write(details[i]+",");
                        fw2.write(details[i+1]+",");
                        fw2.write(details[i+2]+",");
                        fw2.write(speed+"/n");
                        fw2.close(); // file writer closes

                    }
                }

                System.out.println("The numberplate entered could not be found in the file, sorry."); // outputted if registration is not found
            }

任务是搜索文件中的车牌,如果该车牌不在文件中,则应输出该车牌不在文件中

the output of the code above is: the speed the car was driving at was: 39.47 mph Name: Adam Davies Registration: AA12ASD Address Line 1: 1 New Road Address Line 2: Northfleet The numberplate entered could not be found in the file, sorry.


共 (2) 个答案

  1. # 1 楼答案

    所以我要做的是,如果找到了细节,我会添加一个布尔检查

    从一开始就把它设置为false。 那什么时候

    if(searchword.equals(details[i]))
    

    使布尔值为true。 在for循环之后,检查布尔值是否为false,如果为false,请打印您的抱歉,因为找不到这个车牌

    下面的一个简单示例如下所示:

    boolean yes = false;
        for (int i=0;i<details.length-2;i=i+1){
            if(searchword.equals(details[i])){
                System.out.println("Name: " +details[i-1]);
                yes = true;
            }
        }
        if (!yes){
            System.out.println("sorry this registration plate couldn't be found")
        }
    }
    
  2. # 2 楼答案

    如果我收集的信息正确,代码的目的是查找文本文件中是否存在特定的注册号。为此,您将整个文件内容读入一个字符串,并在,上拆分它。然后使用for loop遍历整个数组,并查找您拥有的特定注册号,即searchword

    作为记录,我并不完全相信你实施它的方式(本可以更好,我将不涉及)。然而,按照您已经实现的方式,您的方法的问题是,您正在执行一个操作,在for loop中搜索key,并尝试在循环本身中同时执行if-else部分

    这就是你可以做你需要的事情的方式

        String a = "Hello,World,Foo,Bar,abc,def,ghi,Hello1,World1,Foo1,Bar1,abc,def,ghi";
        String[] b = a.split(",");
        String searchWord = "abc";
    
        boolean hasSearchWord = false;
    
        for(int i = 0; i < b.length; i++) {
            if(b[i].equals(searchWord)) {
                hasSearchWord = true;
                System.out.println("Word found");
                // Print whatever you need
                System.out.println(b[i - 1]);
                System.out.println(b[i]);
                System.out.println(b[i + 1]);
            }
        }
    
        if(!hasSearchWord) {
            System.out.println("Word not found");
        }
    

    搜索abc时,这将生成

    Word found
    Bar
    abc
    def
    Word found
    Bar1
    abc
    def
    

    当搜索duh时,这将产生

    Word not found