gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:27:22 26 4
gpt4 key购买 nike

我正在尝试读取一个包含姓名和电话号码的文本文件,该文件中还可以包含其他文本文件(包括它本身)

myBook.txt:

7
name1 123-456-7890
name2 098-765-4321
name3 135-792-4680
name4 246-801-3579
PHONEBOOK-FILE myBook2.txt
name5 147-025-8369
name6 150-263-7495

myBook2.txt:

1
Name7 000-222-3332

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

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

这是我的代码:

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);
}
}

我认为问题出在 doWork 中,但我可能是错的。它所做的是递归文件,直到到达指定的底部,如果没有找到名称,它应该中断递归并继续传递 PHONEBOOK-FILE 行。

目前,如果您搜索名称,则如果返回未找到,则通过该行。它似乎不是从递归中出来的。

你可能会说我对此不太擅长。感谢您的帮助。

最佳答案

对于文件中的每一行,您将计算一个值。要么没有找到,要么是你的电话簿中的一行。如果你得到一条线,你就可以跳出循环。无论哪种方式,在循环之后您都会返回值:您得到的行或未找到的行;

更棘手的是如何计算引用另一个电话簿的行,答案是您只需使用该电话簿调用您的方法即可。这就是递归部分。

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;
}


}

关于java - 跳出循环中的递归函数但让循环继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39737352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com