4

我做了一个对象,MyString。我不知道如何重新创建 valueOf(double d)。我为整数重新创建了 valueOf。为了方便起见,我将小数位数限制为 8。如何重新创建 valueOf(double d)?

public class MyString {

private char[] a;

public MyString(String s) {
    this.a = s.toCharArray();
}

public MyString(char[] a) {
    this.a = a;
}

public String toString() {
    return new String(a);
}

public int length() {
    return a.length;
}

public char charAt(int i) {
    return a[i];
}
    public static MyString valueOf(int i) {
    int digits = (int)(Math.log10(i)+1);
    char[] b = new char[digits];
    for (int j = 0; j < digits; j++) {
        b[j] = (char) (48 + i / 10);
        i = i % 10;
        if (i < 10) {
            b[j + 1] = (char)(48 + i);
            break;
        }
    }
    MyString ms = new MyString(b);
    return ms;
    }
public static MyString valueOf(double d) {
            char[] d1 = new char[digits];
            //Take each digit of the number and enter it into the array
            MyString ms = new MyString(d1);
    return ms;

}

public static void main(String[] args) {

}

}

4

2 回答 2

1

我假设您这样做是为了好玩……所以这是我采取的方法。你已经有了 valueOf(int i) ,所以为什么不基本上重用那个函数。只需取双倍并继续乘以 10 直到你有一个 int。跟踪小数位的位置,然后你基本上调用你的 valueOf(int i) 但也包括小数点。

我在运行你的代码时遇到了麻烦,所以我重新做了 valueOf(int i) 然后创建了 valueOf(int i, int decimalSpot),为小数点传入 -1 或 0 然后它是一个整数值并且不使用小数位.

无论如何,这就是我想出的。已经很晚了,所以可能不是最干净的代码,但应该给你一个概念证明。

public class MyString {

    private char[] a;

    public MyString(String s) {
        this.a = s.toCharArray();
    }

    public MyString(char[] a) {
       this.a = a;
    }

    public String toString() {
        return new String(a);
    }

    public int length() {
        return a.length;
    }

    public char charAt(int i) {
        return a[i];
    }

    public static MyString valueOf(int i) {
        return MyString.valueOf(i,-1);
    }

    public static MyString valueOf(double d) {
        int decimalPlace = 0;

        while (d != (int)d)
        {
            decimalPlace++;
            d = d*10;
        }

        return MyString.valueOf((int)d,decimalPlace);
    }

    public static MyString valueOf(int i, int decimalSpot) {
        int index=0;
        int digits = (int)(Math.log10(i)+1);
        int stringLength=digits;
        if (decimalSpot == 0) decimalSpot=-1; // Don't return 1234. - just return 1234
        if (decimalSpot != -1) 
        {
            // Include an extra spot for the decimal
            stringLength++;
        }
        char[] b = new char[stringLength];
        for (int j = digits-1; j >= 0; j--) {
            int power = (int) Math.pow(10,j);
            int singleDigit = (int) Math.floor(i/power);
            i = i - power*singleDigit;
            b[index++] = (char) (48 + singleDigit);

            if (decimalSpot==j)
            {
                b[index++] = '.';
            }
        }

        MyString ms = new MyString(b);
        return ms;
    }

    public static void main(String[] args) {        
        MyString ms = MyString.valueOf(12345);
        System.out.println(ms);

        ms = MyString.valueOf(12345.12313);
        System.out.println(ms);
    }

}
于 2014-01-16T06:56:52.863 回答
0

而不是试图为每个可能的源数据类型解决这个问题,你应该只专注于从字符串构造你的类。然后,您所要做的就是将此方法以及您尚未完成的所有其他方法委托给每种情况下相应的 String 方法,获取生成的 String,并使用该 String 构造您的对象。

于 2014-01-16T06:30:45.263 回答