2016-12-22 47 views
0

我正在处理一个涉及椭圆曲线的小型个人项目,而且我对曲线的实例变量有点困难。变量在main方法中被正确打印,但print方法总是返回每个变量等于0.有没有人看到一种方法来解决这个问题?请耐心等待,我知道这是一个相当微不足道的问题。简单实例变量问题

public class ellipticcurve { 

public int A, B, p; 
public ellipticcurve(int A, int B, int p) { 
    A = this.A; 
    B = this.B; 
    p = this.p; 
    // E:= Y^2 = X^3 + AX + B 
} 

public static boolean isAllowed(int a, int b, int p) { 
    return ((4*(Math.pow(a, 3)) + 27*(Math.pow(b, 2)))%p != 0); 
} 

public static void printCurve(ellipticcurve E) { 
    System.out.println("E(F" + E.p + ") := Y^2 = X^3 + " + E.A + "X + " + E.B + "."); 
} 

public static void main(String[] args) { 
    ArgsProcessor ap = new ArgsProcessor(args); 
    int a = ap.nextInt("A-value:"); 
    int b = ap.nextInt("B-value:"); 
    int p = ap.nextInt("Prime number p for the field Fp over which the curve is defined:"); 

    while (isAllowed(a, b, p) == false) { 
     System.out.println("The parameters you have entered do not satisfy the " 
       + "congruence 4A^3 + 27B^2 != 0 modulo p."); 
     a = ap.nextInt("Choose a new A-value:"); 
     b = ap.nextInt("Choose a new B-value:"); 
     p = ap.nextInt("Choose a new prime number p for the field Fp over which the curve is defined:"); 
    } 

    ellipticcurve curve = new ellipticcurve(a, b, p); 
    System.out.println(curve.A + " " + curve.B + " " + curve.p); 
    printCurve(curve); 
    System.out.println("The elliptic curve is given by E(F" + p 
      + ") := Y^2 = X^3 + " + a + "X + " + b + "."); 
} 

回答

2

在你的构造函数中它应该是这样的。

public ellipticcurve(int A, int B, int p) { 
    this.A = A; 
    this.B = B; 
    this.p = p; 
    // E:= Y^2 = X^3 + AX + B 
} 

代替

public ellipticcurve(int A, int B, int p) { 
    A = this.A; 
    B = this.B; 
    p = this.p; 
    // E:= Y^2 = X^3 + AX + B 
} 

要指定实例变量在构造函数中传递的变量,因此该实例变量将被初始化为它们的默认值

+0

也做到了,谢谢!这样一个小错误 – wucse19