gpt4 book ai didi

java - 从 Java 程序到 Python 程序的自定义输入

转载 作者:行者123 更新时间:2023-12-04 13:07:52 25 4
gpt4 key购买 nike

我正在尝试制作一个运行 python 程序的成熟 Java 程序。 python程序如下:

print('Enter two numbers')
a = int(input())
b = int(input())
c = a + b
print(c)

如果我执行这段代码,终端看起来像这样:

Enter two numbers
5
3
8

现在,我希望在从 Java 执行这段代码时得到相同的输出。这是我的 Java 代码:

import java.io.*;
class RunPython {
public static void main(String[] args) throws IOException {
String program = "print('Enter two numbers')\na = int(input())\nb = int(input())\nc = a + b\nprint(a)\nprint(b)\nprint(c)";
FileWriter fileWriter = new FileWriter("testjava.py");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(program);
bufferedWriter.close();
Process process = Runtime.getRuntime().exec("python testjava.py");
InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(process.getOutputStream());
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
String output;
while (process.isAlive()) {
while (!bufferedReader.ready());
System.out.println(bufferedReader.ready());
while (!(output = bufferedReader.readLine()).isEmpty()) {
System.out.println(output);
}
bufferedReader.close();
if (process.isAlive()) {
outputStreamWriter.write(in.readLine());
}
}
}
}

但是在运行这个程序时,只显示第一行并接受第一个输入。之后,程序没有响应。我犯了什么错误?解决方案是什么?

最佳答案

处理输入和输出到另一个进程有点乱,你可以阅读关于如何做到的一个很好的答案 here

因此,将这些答案应用到您的代码中可能是这样的:

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

class RunPython {
public static void main(String[] args) throws IOException {
// String program = "print('Enter two numbers')\na = int(input())\nprint(a)\nb = int(input())\nprint(b)\nc = a + b\nprint(c)";
// If you are using Java 15 or newer you can write code blocks
String program = """
print('Enter first number')
a = int(input())
print(a)
print('Enter second number')
b = int(input())
print(b)
c = a + b
print(c)
""";
FileWriter fileWriter = new FileWriter("testjava.py");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(program);
bufferedWriter.close();

Process process =
new ProcessBuilder("python", "testjava.py")
.redirectErrorStream(true)
.start();

Scanner scan = new Scanner(System.in);

BufferedReader pythonOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedWriter pythonInput = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));

Thread thread = new Thread(() -> {
String input;
while (process.isAlive() && (input = scan.nextLine()) != null) {
try {
pythonInput.write(input);
pythonInput.newLine();
pythonInput.flush();
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
process.destroy();
System.out.println("Python program terminated.");
}
}
});
thread.start();

String output;
while (process.isAlive() && (output = pythonOutput.readLine()) != null) {
System.out.println(output);
}
pythonOutput.close();
pythonInput.close();
}
}

请注意,pythonInput 在 Java 中是一个 BufferedWriter,反之亦然pythonOutput 是一个 BufferedReader。

关于java - 从 Java 程序到 Python 程序的自定义输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68543514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com