Python与Java代码不起作用

时间:2013-04-03 09:05:27

标签: java eclipse python-3.x jython-2.5

我正在尝试执行Python程序(用Python 3.x编写)来比较两个数字。 IDE:Eclipse JAR:jython-standalone-2.5.3.jar
当我运行程序时,在Eclipse的控制台中它说:        “请输入第一个整​​数:” 输入整数后,程序不再执行。 可能有什么问题? 注意:它在Python IDLE中运行良好。

   JAVA CODE:(ExperimentWithPython.java)
   ===============
   import org.python.core.PyException;
   import org.python.util.PythonInterpreter;
   public class ExperimentWithPython{
    public static void main(String[] args) throws PyException{   
    try
    {
     PythonInterpreter.initialize(System.getProperties(), System.getProperties(),new                String[0]);
        PythonInterpreter interp = new PythonInterpreter();
        interp.execfile("D:/Development/comparisons01.py");
    }
    catch(Exception e)
    {
        System.out.println(e.toString());
        e.printStackTrace();
       }
      }
    }

    PYTHON CODE: (comparisons01.py)
     ===============
     print("Enter two integers and I will tell you")
     print("the relations they satisfy")
     number1 = input( "Please enter the first integer: " )
     number1 = int(number1)
     number2 = input( "Please enter the second integer:" )
     number2 = int(number2)
     if number1 == number2:
     print ("%d is equal to %d" % (number1, number2))
     if number1 != number2:
     print ("%d is not equal to %d" % (number1, number2))
     if number1 < number2:
     print ("%d is less than %d" % (number1, number2))
     if number1 > number2:
     print ("%d is greater than %d" % (number1, number2))
     if number1 <= number2:
     print ("%d is less than or equal to %d" % (number1, number2))

      if number1 >= number2:
      print ("%d is greater than or equal to %d" % ( number1, number2 ))
     dummy=input()

我发现解决方案实际上由于代码而导致无法正常工作jar兼容性。

我在Python 2.x标准上重新编写代码,安装了Python 2.7&amp;在我的构建路径中使用jython-standalone 2.7 jar。 它工作正常。 您可以尝试以下代码:

     import random
     import time

    ##Declare Variables
     user_num=0
    ##lottery_num=random.randint(10,99)
    lottery_num=12

     ##Input
       print("Welcome to the Lottery Program!")
       user_num=int(input("Please enter a two digit number: "))
       print("Calculating Results.")
       for i in range(3):
        time.sleep(1)
        print(".")

    ##Calc & Output
     lottery_tens = lottery_num // 10
     lottery_ones = lottery_num % 10

     user_tens = user_num // 10
     user_ones = user_num % 10

     if lottery_num == user_num:
     print("All your numbers match in exact order! Your reward is $10,000!\n")
      elif lottery_tens == user_ones and lottery_ones == user_tens:
    print("All your numbers match! Your reward is $3,000!\n")
     elif lottery_tens == user_tens or lottery_ones == user_ones \
     or lottery_ones == user_tens or lottery_tens == user_ones:
      print("One of your numbers match the lottery. Your reward is $1,000!\n")
    else:
      print("Your numbers don't match! Sorry!")

0 个答案:

没有答案