2017-06-22 27 views
0

我知道這是一個noob程序,但我慢慢地感到困惑。 '下'功能將像CD一樣行事,「上」功能將像CD一樣行事。如何繼續製作 - BlueJ Java文件管理器

我不知道如何讓用戶創建文件或文件夾。我試圖使用arrayLists而不是數組,但無法排除錯誤。任何幫助,將不勝感激。

import java.util.Scanner; 
    class FileManager { 
    //array of arrays will go here 
    String Dir[] = {"UserOne"}; 
    String SystemFolders [] = {"Documents","","",}; 
    String SubFiles [] = {"","","","","",""}; 
    String Nav [][] = { Dir, SystemFolders, SubFiles}; 
    int levelCounter = 0; 
    public void main(String[]args) { 
     Scanner sc = new Scanner (System.in); 
     System.out.println("Enter a command"); 
     String command = sc.next(); 
      if (command.compareTo("down") == 0) 
       down(); 
      //else if is on the way 
    } 
    void down() { 
     //This will execute when the command is 'down' 
     System.out.println(Nav[++levelCounter]); 
    } 
    void up() { 
     //This will execute when the command is 'up'. It acts like cd.. 
     System.out.println(Nav[--levelCounter]); 
    } 
} 

回答

0

如果這是你的程序的入口點,那麼你需要聲明你main方法爲靜態,像這樣

然後訪問您的主要方法,在你的FileManager類的方法你需要在你的main方法中創建你的類的一個實例。像這樣

public static void main(String[]args) { 
    FileManager fm = new FileManager(); // Creates an instance 
    Scanner sc = new Scanner (System.in); 
    System.out.println("Enter a command"); 
    String command = sc.next(); 
    if (command.equals("down")) // equals will suffice in this case 
           // or equalsIgnoreCase() if you dont want case to be a problem 
     fm.down(); // Notice now this calls the down method from the instance 
    //else if is on the way 
} 

然後看看這例子create files或這create folders