我正在創建一個程序,它將接受源代碼,編譯它並輸入測試用例以查看程序是否正確。一個源代碼檢查器,如果可以的話。我用來編譯程序的是通過cmd。我現在的問題是如果程序在cmd中運行,如何輸入測試用例。 這實際上是我們學校的一門課程。所以教授會給出一個問題(例如輸入一個整數,如果是偶數或奇數),那麼這個程序將通過測試教授提供的測試用例來檢查學生的源代碼(例如input:1 output:odd,輸入2:輸出:偶數)。如何在使用java在cmd中運行的程序中輸入
這裏是我的示例代碼(C#編譯器)
case ".cs":
CsCompiler();
Run(path + "\\program");
break;
我的功能:
public static void CsCompiler() throws IOException, InterruptedException {
Process(path + "\\", " c:\\Windows\\Microsoft.NET\\Framework\\v3.5\\csc /out:program.exe *.cs");
}
public static void Process(String command, String exe) throws IOException, InterruptedException {
final Process p;
if (command != null) {
p = Runtime.getRuntime().exec(exe, null, new File(command));
} else {
p = Runtime.getRuntime().exec(exe);
}
new Thread(new Runnable() {
public void run() {
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ((line = input.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
p.waitFor();
}
public static void Run(String command) throws IOException, InterruptedException {
String[] argss = {"cmd", "/c", "start", command};
ProcessBuilder pb;
pb = new ProcessBuilder(argss);
pb.start();
}
如何進入測試案例?你能提供關於這個問題的更多細節嗎? –
@KevinHooke編輯:) – phoenixWright