2016-12-22 79 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