我需要拉数字位置(如十分位数,百分位数等...)

时间:2015-10-02 02:02:15

标签: java

我需要从客户输入的数字中抽出一个特定数字的位置,比如十分之一。例如,他们输入数字156,如果我需要十分之一的位置,他们会拔出5或者如果他们需要百分之一的地方就会退出1.

这是我的第一个Java编程课程,所以我们还没有完成高级的东西,但是到目前为止我还有这个:

    /**
 * @author Eddie Maiale
 * @version October 1st, 2015
 */
public class LcdDigit {
    //Digit display field
    private int digitValue;

    public LcdDigit() {
        digitValue = 0;
    }

    public LcdDigit (int newDigitValue, int place) {
        if (newDigitValue >  0) {
            digitValue = newDigitValue;
        }

        if (place < 10) {
            digitValue = digitValue % 10;
        } else if (place < 100) {
            digitValue = digitValue % 100;
        } else {
            digitValue = digitValue % 1000;
        }

    }

    public int getDigitValue() {
        return digitValue;
    }

    public void setDigitValue (int newDigitValue, double place) {
        if (newDigitValue > 0) {
            digitValue = newDigitValue;
        }

        if (place < 10) {
            digitValue = digitValue % 10;
        } else if (place < 100) {
            digitValue = digitValue % 100;
        } else {
            digitValue = digitValue % 1000;
        }
    }

    public String returnString() {
        return "" + digitValue;
    }
}

这是客户端代码:

class LcdDigitDriver {


 /** The main method where the program starts. */
    public static void main(String[] args)
    {
        LcdDigit noArgs = new LcdDigit();
        LcdDigit onesPlace = new LcdDigit(456, 1);
        LcdDigit tensPlace = new LcdDigit(456, 10);
        LcdDigit hundredsPlace = new LcdDigit(456, 100);
        System.out.println(noArgs.getDigitValue());
        System.out.println(onesPlace.getDigitValue());
        System.out.println(tensPlace.getDigitValue());
        System.out.println(hundredsPlace.getDigitValue());
        System.out.println(hundredsPlace.returnString());
        hundredsPlace.setDigitValue(892, 100);
        System.out.println(hundredsPlace.getDigitValue());
    }
}

输出需要如下所示:

0
6
5
4
4
8

编辑:当前输出如下:

0
6 
56 
456 
456 
892

3 个答案:

答案 0 :(得分:0)

我建议使用某种for循环来查看每个位置值。我想你可以使用.length来查找字符串/数字中有多少个数字。所以,156,有3.从那里,你可以从索引2递减到0.我说2因为在java中,索引从0开始并从那里上升。在这种情况下,0索引是1,1索引是5,2索引是6.我们知道第一个值是我们的“1”,从那里你可以有一个数组,它接收所有这些值。因此,你有每个值等等。从那里你可以用这些数字做任何你需要的事情。

答案 1 :(得分:0)

您可以将构造函数更改为:

public LcdDigit (int newDigitValue, int place) {
    if (newDigitValue >  0) {
        digitValue = newDigitValue;
    }

    if (place == 1) {
        digitValue = digitValue % 10;
    } else if (place == 10) {
        digitValue = (digitValue / 10) % 10;
    } else {
        digitValue = (digitValue / 100) % 10;
    }

}

您还必须将setDigitValue方法更改为类似的方法。

这可以通过将您要查找的数字移动到该位置来实现,然后通过使用模数运算符轻松找到该数字。例如,如果您的数字值为230并且您正在尝试查找该位置,那么您将首先除以10。结果值为2323 % 10等于3,这是原始值的十位数。

答案 2 :(得分:0)

您可以通过检查乘数(十分之一,数百等)并使用此方法来实现:

public static void main(String[] args) {
    // will show the 'thousands' in 1234
    System.out.println(getTheNthDigitFromNumber(1234,1000));

    // will show the 'hundreds' in 235464
    System.out.println(getTheNthDigitFromNumber(235464,100));
}

public static char getTheNthDigitFromNumber(int number,int multiplier){
    int thePosition=0;
    while(multiplier > 0){
        thePosition++;
        multiplier /= 10;

    }
    // converting to string and finding the char from the end
    String theNumber=String.valueOf(number);
    return theNumber.charAt(theNumber.length() - thePosition);
}