0

我正在尝试使用 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))
4

0 回答 0