1

我是这个网站的新手,我是 java 的初学者。我正在制作一个程序,它需要 5 个数字并将它们吐出。为什么这段代码不起作用?这是 2 中的第一个 .java。我在我认为问题所在的地方做了标记。

public class digitExtractor {
    int tNumber;
    String sNumber;

    // OP thinks the problem is here...
    public digitExtractor(int tNumber) {       


    }
    // ... and here
    public digitExtractor(String sNumber) {



    }

    public void returnInvertedOrderByMath() {
        int Integer1;
        int Integer2;
        int Integer3;
        int Integer4;
        int Integer5;
        Integer1 = (tNumber % 10);
        Integer2 = (tNumber % 100) / 10;
        Integer3 = (tNumber % 1000) / 100;
        Integer4 = (tNumber % 10000) / 1000;
        Integer5 = (tNumber % 100000) /10000;

        System.out.println(Integer1 + "\n" + Integer2 + "\n" + Integer3 + "\n" + Integer4 + "\n" + Integer5);
    }

    public void returnInvertedOrderByString() {
        char part1;
        char part2;
        char part3;
        char part4;
        char part5;
        part1 = sNumber.charAt(4);
        part2 = sNumber.charAt(3);
        part3 = sNumber.charAt(2);
        part4 = sNumber.charAt(1);
        part5 = sNumber.charAt(0);
        System.out.println(part1 + "\n" + part2 + "\n" + part3 + "\n" + part4 + "\n" + part5);

    }
}
4

3 回答 3

0

这应该起作用,请记住,我在您的程序中添加了一个 main 方法来测试您的代码并查看我添加到您的代码中的注释。

public class DigitExtractor {
    private int tNumber;
    private String sNumber;

    public static void main(String[] args){

        //this will work because i chose the right constructor
        DigitExtractor d = new DigitExtractor(10000);
        d.returnInvertedOrderByMath();

        //this wont work because sNumber is null
        //d.returnInvertedOrderByString
    }

    public DigitExtractor(int tNumber) {
        this.tNumber = tNumber;
    }
    public DigitExtractor(String sNumber) {
        this.sNumber = sNumber;
    }

    public void returnInvertedOrderByMath() {
        int Integer1 = (tNumber % 10);
        int Integer2 = (tNumber % 100) / 10;
        int Integer3 = (tNumber % 1000) / 100;
        int Integer4 = (tNumber % 10000) / 1000;
        int Integer5 = (tNumber % 100000) /10000;

        System.out.println(Integer1 + "\n" + Integer2 + "\n" + Integer3 + "\n" + Integer4 + "\n" + Integer5);
    }

    public void returnInvertedOrderByString() {
        char part1 = sNumber.charAt(4);
        char part2 = sNumber.charAt(3);
        char part3 = sNumber.charAt(2);
        char part4 = sNumber.charAt(1);
        char part5 = sNumber.charAt(0);

        System.out.println(part1 + "\n" + part2 + "\n" + part3 + "\n" + part4 + "\n" + part5);

    }
}
于 2013-08-05T17:27:31.347 回答
0
int tNumber; // This is an instance variable as it belongs to the class
String sNumber;

// This is a constructor which takes an integer
public digitExtractor(int tNumber) {  // tNumber here is a parameter which is not the same as tNumber above. You can change the name of this parameter to anything you like     
    // "this" keyword tells us we are referring to instance variable and assigning the value of tNumber parameter to instance one
    this.tNumber = tNumber; 
}

public digitExtractor(String sNumber) {
    if (sNumber == null){
        this.sNumber = ""; // if null, let's put an empty string as default
    } else {
        this.sNumber = sNumber;
    }
}
于 2013-08-05T17:31:54.430 回答
0

我认为您想要的是在构造函数中初始化实例字段。

public digitExtractor(int tNumber)中,您将拥有这样的身体:

// assigning instance field with constructor's argument value
this.nNumber = tNumber; 

public digitExtractor(String sNumber)中,您将拥有这样的身体:

// assigning instance field with constructor's argument value
this.sNumber = sNumber; 

这样,您returnInvertedOrder将引用DigitExtractor已初始化的字段,然后执行您想要执行的操作。

请注意,这对于该字段尤其重要,因为如果在代码中引用它之前尚未初始化它String,您将抛出 a 。NullPointerException

如果未初始化,int则默认为。0

于 2013-08-05T17:11:43.673 回答