2012-03-14 65 views
1

前幾天我在這裏發佈了一個關於這個文件的帖子,它有一些很好的答案/想法,我還沒有實現,因爲我一直在工作,喝酒,沒有有時間去解決它(哈哈)。無論如何,我重新發帖的原因是因爲我有點討厭這個問題。我遇到的問題是我的while(_active)循環,我用這個包裝了我的Main類和我的commandCreate類,只需將布爾值更改爲false即可輕鬆退出文件。但是,在我的commandCreate類中,布爾值設置爲false,雖然它是假的,但我不能進入創建部分,因爲它不允許我,它只是繼續返回到Main,其中 - 就像我將布爾值更改爲true,它會自動將我直接帶到Create部分,完全跳過Main類,即使我啓動到Main類中。如果有人能夠幫助我,我的Main和commandCreate類都在下面。布爾故障(以我的名義)

不建議對我來說,我commandCreate改變的方法,以及改變周圍的,現在什麼,我只想得到這個固定

Main.java

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

public class Main extends API { 
     private boolean _active = true; 
    String _username = System.getProperty("user.name").toLowerCase(); 
    String _os = System.getProperty("os.name").trim().toLowerCase(); 

    CommandCreate create = new CommandCreate(); 

    public Main() { 
     try { 
      while(_active) { 
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
       print(username() + "@" + os() + ":~$ "); 
       String command = br.readLine(); 
        if(command.equalsIgnoreCase("create")) { 
         new CommandCreate(); 
        /*} else if(command.equals("compile")) { 
         new CommandCompile();*/ 
        } else if(command.equalsIgnoreCase("help")) { 
         println("Commands"); 
         println(" create    - Creates .java files, does not compile."); 
         //println(" compile    - Creates .java files, compiles on creation."); 
         println(" exit    - Exits program"); 
         println(" help    - Shows help documentation."); 
        } else if(command.equalsIgnoreCase("exit")) { 
         /*print("Are you sure you want to exit? (Y/N) "); 
         String exit = br.readLine(); 
         if(exit.equalsIgnoreCase("y")) { 
         exit();*/ 
         _active = false; 
         /*} else { 
         println("Cancelled!"); 
         }*/ 
        } else if(command.isEmpty()) { 

        } else { 
         println("\"" + command + "\" does not exist. Please review the \"help\" menu"); 
        } 
      } 
     } catch(IOException ex) { 
      ex.printStackTrace(); 
      } 
    } 

    public static void main(String[] args) { 
    new Main(); 
    } 
} 

commandCreate.java

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

public class commandCreate { 
    boolean _active = true; 
    String _username = System.getProperty("user.name").toLowerCase(); 
    String _os = System.getProperty("os.name").trim().toLowerCase(); 
    String fileName, create, option; 

    public commandCreate() { 
     try { 
     while(_active) { 
      System.out.print(_username + "@" + _os + ":~/create$ "); 
      Scanner kbd = new Scanner(System.in); 
       String userLine = kbd.nextLine(); 

      if(java.util.regex.Pattern.matches(".*\\S\\s+\\S.*", userLine)) { 
        Scanner read = new Scanner(userLine); 
         option = read.next(); 
         fileName = read.next(); 
      } 

      FileWriter create = new FileWriter(new File("Created Files/" + fileName + ".java")); 

      if(userLine.equals(option + " " + fileName)) { 
       if(option.equals("-a")) { 
        // Option = -a, creates standard file with main class. 
        create.write("public class " + fileName + " {\n"); 
        create.write(" public static void main(String[] args) {\n"); 
        create.write("  System.out.println(\"Welcome to your new program!\");\n"); 
        create.write(" }\n"); 
        create.write("}"); 
       } else if(option.equals("-c")) { 
        // Option = -c , creates standard file with overloaded constructor & main class. 
        create.write("public class " + fileName + " {\n"); 
        create.write(" public " + fileName + "() {\n"); 
        create.write("  System.out.println(\"Welcome to your new program!\");\n"); 
        create.write(" }\n"); 
        create.write("\n"); 
        create.write(" public static void main(String[] args) {\n"); 
        create.write("  new " + fileName + "();\n"); 
        create.write(" }\n"); 
        create.write("}"); 
       } else if(option.equals("-j")) { 
        // Option = -j, creates GUI within constructor w/ single JLabel. 
        create.write("import javax.swing.*;\n"); 
        create.write("import java.awt.*;\n"); 
        create.write("import java.awt.event.*;\n"); 
        create.write("\n"); 
        create.write("public class " + fileName + " extends JFrame {\n"); 
        create.write(" private static final int HEIGHT = 50;\n"); 
        create.write(" private static final int WIDTH = 400;\n"); 
        create.write("\n"); 
        create.write(" private JLabel welcomeJ;\n"); 
        create.write("\n"); 
        create.write(" public " + fileName + "() {\n"); 
        create.write(" super(\"Welcome to your program - " + fileName + "\");\n"); 
        create.write("  Container pane = getContentPane();\n"); 
        create.write(" setLayout(new FlowLayout());\n"); 
        create.write("\n"); 
        create.write("  welcomeJ = new JLabel(\"Welcome To Your Program!\", SwingConstants.CENTER);\n"); 
        create.write("\n"); 
        create.write("  pane.add(welcomeJ);\n"); 
        create.write("\n"); 
        create.write("  setSize(WIDTH, HEIGHT);\n"); 
        create.write("  setVisible(true);\n"); 
        create.write("  setResizable(false);\n"); 
        create.write("  setDefaultCloseOperation(EXIT_ON_CLOSE);\n"); 
        create.write(" }\n"); 
        create.write("\n"); 
        create.write(" public static void main(String[] args) {\n"); 
        create.write("  new " + fileName + "();\n"); 
        create.write(" }\n"); 
        create.write("}"); 
       } 
      } else if(userLine.equalsIgnoreCase("help")) { 
       System.out.println("Commands"); 
       System.out.println(" Syntax: [-option] [filename]"); 
       System.out.println("  -a [filename]  [Program: main class]"); 
       System.out.println("  -c [filename]  [Program: overloaded constructor, main class]"); 
       System.out.println("  -j [filename]  [Program: GUI: overloaded constructor, main class]"); 
      } else if(userLine.equalsIgnoreCase("exit")) { 
       System.exit(0); 
      } else { 
       System.out.println("Error in syntax. Please review the \"help\" menu"); 
      } 
      create.close(); 
     } 
     } catch(IOException e) { 
      System.out.println("There was an error: " + e); 
     } catch(InputMismatchException ex) { 
      System.out.println("There was an error: " + ex); 
     } 
    } 

    public static void main(String[] args) { 
     new commandCreate(); 
    } 
} 
+2

偏題:爲什麼你在變量前加下劃線?不是公約的一部分。 – LuckyLuke 2012-03-14 08:25:06

+0

你能鏈接到你以前的問題嗎? – 2012-03-14 08:29:17

+0

while(true)替換while循環(_active),當你想終止你的程序時使用System.exit(0)。 無論如何,你的問題是你使用兩個單獨的_active變量,並且當你在commandCreate上設置_active爲false時 - main看不到變化,導致它使用自己的變量 – SirVaulterScoff 2012-03-14 08:29:30

回答

0

我不知道我正確理解你的問題,但也有一些事情,真的很奇怪,我和我也有你有誤解,有印象。

首先,在_active變量MaincommandCreate(順便說一句,根據約定類名應以大寫字母開頭)是完全獨立的,你必須手動同步,如果你希望他們相互依賴其他。

其次,您似乎在兩個地方處理了一些命令,例如,MaincommandCreate都處理"exit"命令。從我想要達到的目標來看,只有Main(或更好的專用​​)才能處理全局命令。因此"exit"在執行commandCreate(它不應該停止應用程序本身)時應該有不同的含義,或者您可能想要插入另一個命令以指示"create"命令應該完成。

但是你處理的命令在Main循環還應當檢查他們的狀態,是唯一一個停止應用程序,也許首先做一些清理工作。如果您將"exit"命令保留在commandCreate之內,它可能會以適當的方式通知命令處理器(您的案例中的Main)(偵聽器回調,設置某些標誌等)。

編輯

另外一個觀察:你不創造Main任何實例不斷,因此在構造函數中環路(其爲@Andreas_D已經指出的是壞的話)永遠不會執行。

2

A 問題是你在你的類構造函數中做了一切。構造函數體只能包含創建類實例所必需的代碼。永遠不要把應用程序邏輯放在構造函數中

Main中,您初始化create字段時會出現CommandCreate代碼在您創建Main實例時執行的(側向)效應。而這種情況發生在之前代碼Main中的類構造函數被執行。

通過清除構造函數並將代碼移到CreateCommandMain上的方法來修復它。

+2

(並永遠不會告訴我們,你沒有使用我們的答案,並重新發佈一個問題,因爲你是喝酒太忙) – 2012-03-14 08:43:47