0

我必须编写一个创建信用卡号码的程序。我写了 3 个类,主要类,我可以从中获取卡的类型和长度(例如 Visa 15 位数字)、luhn 公式和一个创建卡的类。从主课我将卡片的类型和长度发送到最后一节课。所以我像这样开始了最后一堂课(不是所有的代码,只有第一行):

public class Cards extends Program {

private int[] x = new int[length];
private int[] k = new int[length];
private int length; 
private String type;

public Cards(int l, String name) {
    length = l;
    type = name;
}

这显然是错误的(编译时出错),但问题是只有我的电脑(我也在其中编写了这些类)才能运行它,而不会出现编译错误并获得正确的结果。我知道问题出在哪里,其他 2 个类是正确的,但我想知道如何在我的电脑上运行而不会出现问题。

Ps:我用的是acm包,我的jdk是1.8.0_31,我写在notepad++上

4

2 回答 2

0

I have the same version of the JDK, and class Card does not compile. You are probably executing against a previously compiled .class . Are you sure your code never was something like:

public class Cards extends Program {

    private int[] x;
    private int[] k;
    private int length; 
    private String type;

    public Cards(int l, String name) {
        length = l;
        x = new int[length] ;
        k = new int[length] ;
        type = name;
    }
}

I bet it was! Try again after removing any file Cards.class in your PC.

于 2015-03-02T23:51:08.710 回答
0

在我的编译器中,它给出“在定义之前无法引用字段”。

您可以将它们定义为:

private int length; 
private int[] x = new int[length];
private int[] k = new int[length];

但是,该代码不清楚,因为length在构造函数中分配了。

也许写得最清楚:

public class Cards extends Program {
    private int[] x;
    private int[] k;
    private int length; 
    private String type;
    public Cards(int l, String name) {
        this.length = l;
        this.x = new int[length];
        this.y = new int[length];
        this.type = name;
    }
}
于 2015-03-02T23:51:23.447 回答