我该如何结束这个循环

时间:2015-10-06 02:00:10

标签: java loops for-loop

是的,这是一个学校的受助人,现在我觉得很愚蠢

我试图让程序计算目录中的文件,将该值存储在变量 RecipeCount 中,然后运行循环以获取计数文件和列表的文件名他们(工作得很好)

当我添加下一行代码为每个抓取的文件添加一个整数时,我发现自己陷入了循环

import java.util.Scanner;
import java.io.File;

public class RecipeBook {
public static void main(String[] args) {

    //Variables 
    String CarriageReturn = System.getProperty("line.separator"); //This will retrieve line separator dependent on OS

    String RecipeDirectory;
    int RecipeCount;
    Scanner UserInput = new Scanner(System.in);
    int RecipeLoopCounter= 0; 

    //Display Application info

    System.out.println("Recipe Book V0.13" + CarriageReturn);

            //Locate Recipe Directory and count Recipes

            System.out.println("First Run Detected!"+ CarriageReturn +"Where are your recipes located?");

                 RecipeDirectory =  UserInput.nextLine();
                 RecipeCount = new File(RecipeDirectory).list().length;


            System.out.println("Folder Scanned - Found " + RecipeCount + " Recipes");
            System.out.println("--------------------Recipe Index--------------------");


            File directory = new File(RecipeDirectory);

            //get all the files from a directory
            File[] fList = directory.listFiles();
                 for (File file : fList){
                 if (file.isFile()){
            System.out.println(file.getName());

            System.out.println("Enter 1-"+ RecipeCount + "to select a recipe card" );

1 个答案:

答案 0 :(得分:1)

//get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList){
    if (file.isFile()){
        System.out.println(file.getName());
    }// end if 
}// end for 

System.out.println("Enter 1-"+ RecipeCount + "to select a recipe card" );

这应该有用。