跳出循环中的递归函数,但让循环继续

时间:2016-09-28 03:02:25

标签: java recursion

我试图从一个文本文件中读取,该文件的名称和电话号码也可以包含其他文本文件(包括它自己)

myBook.txt:

1
Name7 000-222-3332

myBook2.txt:

import java.util.*;
import java.io.*;

public class Phonebook
{
private boolean DEBUG = true;
private Scanner scan;
private Scanner input;
private File file;
private File holder;
private String query;
private boolean bottomOut;
private int nameCount;
private String fileNameHold;

// entry point for class
public void run()throws IOException
{
    input = new Scanner(System.in); 

    //Gets file name and checks if it exists valid file
    while(true)
    {
        System.out.print("Name of phone book to read in: ");
        fileNameHold = input.next();
        file = new File(fileNameHold);
        if(file.exists())
            break;
        else
            System.out.println("That file does not exist!");
    }   
    System.out.println("Phonebook successfully read in!");

    //Main control loop
    while(true)
    {
        bottomOut = false;
        System.out.print("Please enter person to search for: ");
        query = input.next();
        if(query.equals("."))
            break;
        file = new File(fileNameHold);
        System.out.println(doWork(query, file, 0));
    }

    System.out.print("Thank you for using this program!");
    }

    //Does the searching and recursive stuff
    private String doWork(String query, File fileName, int level)throws IOException
    {
    scan = new Scanner(fileName);

    //Grabs item count fom begining of file
    //if(!bottomOut)
    nameCount = Integer.parseInt(scan.nextLine());
    String line = "";

    //Runs through entries
    for(int i=0; i<nameCount; i++)
    {   
        line = scan.nextLine();
        debug("file: " +file);
        debug("line: " + line);
        debug("nameCount: " + nameCount);

        if(line.toLowerCase().contains(query.toLowerCase()))
        {

            return line;
        }
        //Recursion is used to searth through linked files
        else if(line.contains("PHONEBOOK-FILE"))
        {
            //System.out.println("Sanity Check");
            holder = new File(line.replace("PHONEBOOK-FILE ", ""));
            if(level < 2 || (level > 0 && bottomOut))
                return doWork(query, holder, ++level);

            else if(level >= 2 && !bottomOut)
                bottomOut = true;

            else
                return "not found (REC)";

        }

    }
    return "not found";
    }

    private void debug(String stuff)
    {
        if(DEBUG)
            System.out.println("[[--DEBUG--]] " + stuff);
    }
}

第一行是文件中的项目数,然后它有PHONEBOOK-FILE来表示另一个文件。

我无法使用数组,我无法更改myBook.txt,我无法使用try / catch,我必须使用递归

这是我的代码:

def use_magic(target, spell):
    if spell.spell_type == "buff":
        stat = spell.stat
        setattr(target, stat, getattr(target,stat) + spell.value)

我认为这个问题是在doWork中,但我可能错了。它正在做的是它通过文件递归,直到它到达指定的底部,如果它没有找到它应该突破递归的名称并继续通过PHONEBOOK-FILE行。

目前,如果未找到返回,则搜索传递该行的名称。它似乎没有出现在递归中。

你可能会告诉我这个不太好。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

对于文件中的每一行,您将计算一个值。找不到,或者是电话簿的一行。如果你得到一条线,你可以摆脱循环。无论哪种方式,在循环之后,您返回值:您获得或未找到的行;

更复杂的是如何计算引用另一个电话簿的线路,答案是您只需使用该电话簿调用您的方法。这是递归部分。

import java.util.*;
import java.io.*;

public class Phonebook
{
private Scanner input;
private File file;
private String query;

// entry point for class
public void run()throws IOException
{
    input = new Scanner(System.in); 

    //Gets file name and checks if it exists valid file
    while(true)
    {
        System.out.print("Name of phone book to read in: ");
        fileNameHold = input.next();
        file = new File(fileNameHold);
        if(file.exists())
            break;
        else
            System.out.println("That file does not exist!");
    }   
    System.out.println("Phonebook successfully read in!");

    //Main control loop
    while(true)
    {
        bottomOut = false;
        System.out.print("Please enter person to search for: ");
        query = input.next();
        if(query.equals("."))
            break;
        file = new File(fileNameHold);
        System.out.println(doWork(query, file));
    }

    System.out.print("Thank you for using this program!");
    }

    //Does the searching and recursive stuff
    private String doWork(String query, File fileName)throws IOException
    {
    Scanner scan = new Scanner(fileName);
    int nameCount;
    File recurFile;

    nameCount = Integer.parseInt(scan.nextLine());
    String line = "";
    String value = "Not found";
    //Runs through entries
    for(int i=0; i<nameCount; i++)
    {   
        line = scan.nextLine();
        // if the line is a file, then the value of that line
        // is the result to your function applied to that new file
        if(line.contains("PHONEBOOK-FILE")) {
            recurFile = new File(line.replace("PHONEBOOK-FILE ", ""));
            line = doWork(query, holder, ++level);
        }  
        // the file will either return Not found or
        // a line corresponding to your query
        if(line.toLowerCase().contains(query.toLowerCase()))
        {
            // Your line is correct. The function doesn't care where it comes from
            value = line;
            break;
        }

    }
    return value;
    }


}