【问题标题】:LOOK - else if statement: where do i put it看 - else if 语句:我把它放在哪里
【发布时间】:2016-01-28 14:18:09
【问题描述】:

我不知道把我的 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 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.

【问题讨论】:

  • @Rakesh - 我还是不明白,我读了另一个问题,他们说把 else 语句放在 for 循环之外,但这让它每次都输出,我需要一个布尔值吗?跨度>
  • 很难理解你的应用程序需要做什么,因为这里没有人知道上下文。我不确定你说using the else outside the for loop 是什么意思。没有ifelse 条件永远不会存在,并且它必须始终在if 语句之后。否则,它将无法编译。当您将 else 放在“for 循环之外”时,我什至不知道您的代码是如何编译的。也许有一个预先存在的if 条件而没有其他条件?。
  • 你想看我的代码吗?我真的需要帮助,我不知道我做错了什么
  • 关键是你的问题没有真正的意义。请参阅此链接,了解如何提出最小、完整和可验证的问题:*.com/help/mcve

标签: java eclipse if-statement task eclipse-kepler


【解决方案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】:

    如果我收集正确,代码的目的是查找文本文件中是否存在特定的注册号。为此,您将整个文件内容读入一个字符串并将其拆分为,。然后使用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
    

    【讨论】: