2012-03-14 25 views
1

は、私がここ数日前に、このファイルに関連する上でスレッドを掲載、それは私が働いているので、いくつかの良い答え/私が実装し、まだ、まだ午前のアイデアを持って、飲むと持っていませんまだ周りを回る時間があった(笑)。とにかく、私が再掲示し​​ているのは、私が質問をしたからです。私が抱えている問題はwhile(_active)ループであり、簡単なファイルの終了のためにメインクラスとcommandCreateクラスをラップしました。ブール値をfalseに変更するだけです。しかし、私のcommandCreateクラスでブールがfalseに設定され、それが偽である間、私が作成セクションに行くことができない、それは私がすることはできませんので、それだけでバックIF-として主にいっていますブール値をtrueに変更すると、Mainクラスを起動しても、Mainクラスを完全にスキップしてCreateセクションに自動的に移動します。誰かが私を助けることができるなら、私のメインとcommandCreateの両方のクラスは以下の通りです。ブール障害

私は、私はちょうどこれが

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

Main.java

を固定取得したいメソッドに私のcommandCreateを変更するために、そして今の周りに何も変更するために提案しないでください。 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

+0

前の質問にリンクできますか? –

+0

(_active)while(true)の間にループを置き換え、あなたのプログラムを終了したいときはSystem.exit(0)を使います。 とにかく、2つの別個の_active変数を使用し、commandCreateで_activeを設定している間、mainに変更が表示されず、それ自身の変数が使用されます。 – SirVaulterScoff

答えて

0

私はあなたの質問を正しく理解しているかどうかはわかりませんが、本当に私には奇妙に思えるものがいくつかあります。あなたには誤解があるような印象もあります。すべての

まず、(大文字で始めるべき大会のクラス名によると、ところで、)MaincommandCreate_activeの変数は完全に独立しており、あなたがそれらをそれぞれに依存したい場合は、手動で同期させる必要があるだろうその他。

2番目のコマンドは2つの場所で扱われます。たとえば、MaincommandCreateの両方が"exit"コマンドを処理しています。あなたが達成したいと思っているものから、Main(またはそれ以上の特殊な​​)だけがグローバルコマンドを処理する必要があります。従って"exit"は、commandCreate(アプリケーション自体を停止すべきではありません)を実行している間に別の意味を持つか、別のコマンドをintrocudeして、"create"コマンドを終了する必要があることを示します。あなたはコマンドを扱うしかし

Mainのループは、それらの状態をチェックして、アプリケーションを停止し、多分最初のいくつかのクリーンアップを行うための唯一のものでなければなりません。"exit"コマンドをcommandCreateの内部に置いておくと、適切な方法(リスナーのコールバック、いくつかのフラグの設定など)でコマンドプロセッサ(あなたのケースではMain)に通知することができます。

編集

追加の観察:あなたは、このように(@Andreas_Dがすでに指摘したように、すでに悪い)コンストラクタのループが実行されることはありません、今までにMainの任意のインスタンスを作成していません。

2

大きな問題は、あなたのクラスのコンストラクタに全力を尽くすということです。コンストラクタ本体には、クラスのインスタンスを作成するために不可欠なコードのみが含まれます。決してアプリケーションロジックをコンストラクタに入れないでください。 Main

あなたはMainのインスタンスを作成するたびにCommandCreateコードが実行される(側 - )効果を持つフィールドcreateを初期化します。そして、これはコンストラクタが実行されMainクラスでの前にコードを発生します。

はコンストラクタをクリアしCreateCommandMainのメソッドにコードを移動することによってそれを修正します。

+2

(私たちの答えを使用せず、飲みすぎて忙しい) –