我尝试使用Jython 2.7.2从java文件调用python代码,使用版本3.9调用python文件,但出现错误。Java代码如下:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("/src/main/java/com/example/demo/LeapYear.py");
interpreter.set("year", 2020);
PyObject retVal = interpreter.eval("LeapYear().check_leap_year(year)");
System.out.println(retVal.toString());
我的python代码如下:
class Check_Leap_Year:
def check_leap_year(self, year):
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
1条答案
按热度按时间kpbwa7wx1#
我遇到过这样的问题。解决了只是不使用python语法2.0