如何增加numDigits而不使方法成为非静态?

问题描述 投票:-1回答:2

[BigInteger家庭作业中提供了三段代码,要求我们按照名称所示,将极端大小的整数存储到链接列表中,并给出String输入。

如果给定字符串“ 0054321”,则结果链表将按位置顺序存储1-> 2-> 3-> 4-> 5,而忽略无关紧要的数字。

但是当我尝试遍历字符串时,每次找到有效数字时,我都会尝试将numDigits加1。

BigInteger.java(我现在正在使用的代码)

package bigint;
import sun.security.x509.InvalidityDateExtension;

public class BigInteger {

    boolean negative;
    int numDigits;
    DigitNode front;

    public BigInteger() {
        negative = false;
        numDigits = 0;
        front = null;
    }

    public static BigInteger parse(String integer)
    throws IllegalArgumentException {
        int a = 0;
        int b = 0;
        this.front = new DigitNode(1, null);
        int length = integer.length();
        while (length > 0 && a <= length) {
            if (integer.charAt(a) == '-') {
                this.negative = true;
                a++;
            }
            if (integer.charAt(a) == ' ' && this.numDigits == 0) {
                a++;
            }
            if (integer.charAt(a) == ' ' && this.numDigits == 0) {
                a++;
                continue;
            }
            if (Character.isDigit(integer.charAt(a))) {
                if(integer.charAt(a) == ' ' && this.numDigits == 0) {
                    a++;
                    continue;
                }
                this.numDigits = this.numDigits + 1;

            }

            /* IMPLEMENT THIS METHOD */
        }
        // following line is a placeholder for compilation
        return null;
    }

DigitNode.java(封装链接列表的类,不允许编辑此列表)

package bigint;
public class DigitNode {

    int digit;
    DigitNode next;

    DigitNode(int digit, DigitNode next) {
        this.digit = digit;
        this.next = next;
    }

    public String toString() {
        return digit + "";
    }
}

BigTest.java(用于测试是否分析/添加/乘以方法的单词的测试器类,不允许对此进行编辑)

package bigint;

import java.io.IOException;
import java.util.Scanner;

public class BigTest {

    static Scanner sc;

    public static void parse() 
    throws IOException {
        System.out.print("\tEnter integer => ");
        String integer = sc.nextLine();
        try {
            BigInteger bigInteger = BigInteger.parse(integer);
            System.out.println("\t\tValue = " + bigInteger);
        } catch (IllegalArgumentException e) {
            System.out.println("\t\tIncorrect Format");
        }
    }

    public static void add() 
    throws IOException {
        System.out.print("\tEnter first integer => ");
        String integer = sc.nextLine();
        BigInteger firstBigInteger = BigInteger.parse(integer);

        System.out.print("\tEnter second integer => ");
        integer = sc.nextLine();
        BigInteger secondBigInteger = BigInteger.parse(integer);

        BigInteger result = BigInteger.add(firstBigInteger,secondBigInteger);
        System.out.println("\t\tSum: " + result);
    }

    public static void multiply() 
    throws IOException {
        System.out.print("\tEnter first integer => ");
        String integer = sc.nextLine();
        BigInteger firstBigInteger = BigInteger.parse(integer);

        System.out.print("\tEnter second integer => ");
        integer = sc.nextLine();
        BigInteger secondBigInteger = BigInteger.parse(integer);

        BigInteger result = BigInteger.multiply(firstBigInteger,secondBigInteger);
        System.out.println("\t\tProduct: " + result);

    }

    public static void main(String[] args) 
    throws IOException {

        // TODO Auto-generated method stub
        sc = new Scanner(System.in);

        char choice;
        while ((choice = getChoice()) != 'q') {
            switch (choice) {
                case 'p' : parse(); break;
                case 'a' : add(); break;
                case 'm' : multiply(); break;
                default: System.out.println("Incorrect choice"); 
            }
        }
    }

    private static char getChoice() {
        System.out.print("\n(p)arse, (a)dd, (m)ultiply, or (q)uit? => ");
        String in = sc.nextLine();
        char choice;
        if (in == null || in.length() == 0) {
            choice = ' ';
        } else {
            choice = in.toLowerCase().charAt(0);
        }
        return choice;
    }

}

但是,我得到以下错误:

java:非静态变量,不能从静态上下文中引用它,

对于任何this.numDigits或this.front或this.negative。

[每当我尝试增加numDigits或将整数的值更改为正数时,都会发生。有人请帮忙,Data Structures现在真的在踢我的屁股。

java class data-structures static instantiation
2个回答
0
投票

this是指正在调用方法的实例。静态方法没有关联的实例(通常根本不应该从实例中调用它),因此没有this实例。

但是,您可以在静态方法中创建实例并影响its字段,例如:

public static BigInteger parse(String integer)
throws IllegalArgumentException {
    BigInteger parsedBI = new BigInteger();
    //...
    parsedBI.front = new DigitNode(1, null);
    //...
    parsedBI.numDigits = parsedBI.numDigits + 1;
    //...
}

也(我自己也是SO菜鸟,avoid giving more code in your questions than is needed (minimal reproducible example).


0
投票
public static BigInteger parse(String integer)

parse方法被定义为静态函数,因此它无权访问任何BigInteger实例。 “ this”是没有意义的。尚未创建任何实例。

您可以在BigInteger方法内部创建一个新的parse实例。然后,该对象将可以访问其实例变量,例如numDigits。

bi = new BigInteger()

然后您可以访问bi.numDigits而不是this.numDigits,这没有意义。 bi指向BigInteger的对象实例,而this则不指向任何东西,因为我们不在实例内部,而是在静态方法中。

认为静态parse方法只是一个辅助函数,理论上它可以驻留在类定义之外的某个地方,但是为了方便和清楚起见,它位于类定义之内。它的工作是通过解析文本来创建BigInteger实例。

© www.soinside.com 2019 - 2024. All rights reserved.