请帮我弄清楚当我针对特定文件夹进行搜索时如何计算结果?
另外,如何询问用户是否要进行其他搜索?
// Importing utilities
import java.io.File;
import java.util.*;
public class FileListing
{
public static void main (String[] args)
{
// Creating a Scanner
Scanner keyboard = new Scanner(System.in);
// Specifying search location
File file = new File("D:/Music");
String[] content = file.list();
// Searching for a match
System.out.println("Enter the first few characters of the folder/file to do a lookup");
String userInput = keyboard.nextLine();
// Adding text to say what the user searched for
System.out.println("Below you will find the list of folders/files with a partial match to (" + userInput + ").");
System.out.println();
// Posting the contents
for(String folders : content)
{
if(folders.toUpperCase().startsWith(userInput.toUpperCase()))
{
System.out.println("Name: " + folders);
}
}
}
}
答案 0 :(得分:0)
我会添加一个变量
public function notfoundAction()
{
throw new Zend_Controller_Action_Exception('This page does not exist', 404);
}
在for循环之前,如果它匹配则只增加它。
答案 1 :(得分:0)
这应该让你开始。每次找到匹配项时,我都会递增变量count
。我也在永远循环,所以它不断要求用户提供更多的输入。
// Importing utilities
import java.io.File;
import java.util.*;
public class FileListing
{
public static void main (String[] args)
{
// Creating a Scanner
Scanner keyboard = new Scanner(System.in);
// Specifying search location
File file = new File("D:/Music");
String[] content = file.list();
while(true){
// Searching for a match
System.out.println("Enter the first few characters of the folder/file to do a lookup");
String userInput = keyboard.nextLine();
// Adding text to say what the user searched for
System.out.println("Below you will find the list of folders/files with a partial match to (" + userInput + ").");
System.out.println();
// Posting the contents
int count=0;
for(String folders : content)
{
if(folders.toUpperCase().startsWith(userInput.toUpperCase()))
{
System.out.println("Name: " + folders);
count++;
}
}
}
}
}
答案 2 :(得分:0)
如果您想计算比赛数,可以执行以下操作
int i=0;
// Posting the contents
for(String folders : content)
{
if(folders.toUpperCase().startsWith(userInput.toUpperCase()))
{
System.out.println("Name: " + folders);
i++;
}
}
System.out.println("Total number of results: " + i);`
至于询问用户,请考虑使用以下格式的do-while循环
do{
// your code
// ask user and read his answer on a string called userChoice
}while (userChoice.equals('y'))
尝试我们的建议,你会很容易找到答案!
答案 3 :(得分:0)
使用while循环并提示用户输入短语(例如'退出'),如果他们想要停止。读取用户输入后,检查短语并在符合退出短语的情况下调用中断。
使用罗伯特建议的变量来计算找到的文件总数。