简体   繁体   English

如果一次在if循环中找不到一次,则只能打印一次?

[英]Print not found only once in a do while if loop?

Reading from a file and seeing if it matches the users input if it does it will output the definition of the term however when it is not found I would like it to print not found only once but instead it prints every single loop and I cant figure it out for the life of me. 从文件中读取并查看它是否与用户输入匹配,如果输出,它将输出术语的定义,但是当找不到时,我希望它只打印一次,而不会打印一次,而是无法打印为我的生命而努力。

import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class Task6 {

public static void Main(String[] args) throws IOException {
    Scanner sc = new Scanner (System.in);
    FileReader fr = new FileReader("text.txt");
    BufferedReader br = new BufferedReader(fr);
    int input;
    String term;
    System.out.println("1.Search for a term");
    System.out.println("2.Search for a keyword in the descriptions.");
    System.out.println("3.End");
    input = sc.nextInt();
    boolean found=false;
    if (input == 1) {  // first option
        System.out.println("Choice 1 selected.");
        System.out.println("Please type the term you want to search for.");
        term = sc.next(); 
        do {
            if (line.equalsIgnoreCase(term)) {
                found = true;
                System.out.println("Found "+term);
                System.out.println(br.readLine());
            } else if (found==false); System.out.println("Not Found.");
        } while ((br.readLine()!=null)&(found == false));
        br.close();
    } else if (input == 2) { 
        System.out.println("Choice 2 selected.");
        System.out.println("Please type the key term you want to search for.");
        term = sc.next();
        do {
            if (lineTwo.contentEquals(term)) {
                found = true;
                System.out.println("Found "+term);
                System.out.println(line);
                System.out.println(lineTwo);
            } else System.out.println("Not Found");
        } while ((br.readLine()!=null)&(found == false));
        br.close();
    } else System.out.println("Goodbye.");
  }
}

I only need it for the first option. 我只需要第一个选项。

Remove your else statement and just print the not found at the end of the while loop. 删除您的else语句,只打印在while循环末尾找到的not语句。 You can do the same for option 2 您可以对选项2进行相同的操作

import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class Task6 {

public static void Main(String[] args) throws IOException {
    Scanner sc = new Scanner (System.in);
    FileReader fr = new FileReader("text.txt");
    BufferedReader br = new BufferedReader(fr);
    int input;
    String term;
    System.out.println("1.Search for a term");
    System.out.println("2.Search for a keyword in the descriptions.");
    System.out.println("3.End");
    input = sc.nextInt();
    boolean found=false;
    if (input == 1) {  // first option
        System.out.println("Choice 1 selected.");
        System.out.println("Please type the term you want to search for.");
        term = sc.next(); 
        do {
            if (line.equalsIgnoreCase(term)) {
                found = true;
                System.out.println("Found "+term);
                System.out.println(br.readLine());
            }  
        } while ((br.readLine()!=null) && (found == false)); // use short circuit  here
        if (!found) { // !found is the same as found == false
        System.out.println("Not Found.");
        }
        br.close();
    } else if (input == 2) { 
        System.out.println("Choice 2 selected.");
        System.out.println("Please type the key term you want to search for.");
        term = sc.next();
        do {
            if (lineTwo.contentEquals(term)) {
                found = true;
                System.out.println("Found "+term);
                System.out.println(line);
                System.out.println(lineTwo);
            } else System.out.println("Not Found");
        } while ((br.readLine()!=null)&(found == false));
        br.close();
    } else System.out.println("Goodbye.");
  }
}

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

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