简体   繁体   English

为什么在将数字转换为整数的前面得到“ 0”?

[英]Why I am getting “0” in front part of transforming digits into integer?

Example: when you type "1234567", it`s display "0 1 2 3 4 5 6 7". 示例:当您键入“ 1234567”时,其显示为“ 0 1 2 3 4 5 6 7”。 How can I rid of "0" in front part? 如何消除前面的“ 0”?

public static void main(String[] args){

    int num = 0;
    int eachNum = 0;
    Scanner input = new Scanner(System.in);

    System.out.print("Enter your number here: ");
    num = input.nextInt();

    int num2 =  num;
    int length = String.valueOf(num).length();
    while ( length >= 0 ) {

        int substraction =(int) Math.pow(10, length);
        int sub = (int) substraction; 
            eachNum = num2 / sub % 10;
            System.out.print(eachNum + "\t");
            length--;

    }

}//end main method

You could solve this by doing: 您可以通过以下方法解决此问题:

while (length > 0) { // iterating one less time
    int substraction = (int) Math.pow(10, length-1); // length-1 to get from first digit
    ...
}

Anyway you can improve your code and do little trick even, so you don't need the calculations: 无论如何,您都可以改进代码,甚至不做任何技巧,因此不需要计算:

Scanner input = new Scanner(System.in);
System.out.print("Enter your number here: ");    
int num = input.nextInt();  

String str = String.valueOf(num);
for (int i = 0; i < str.length(); i++) {
    System.out.print(str.charAt(i)+"\t");
}

Consider the first iteration. 考虑第一次迭代。

length is 7 . length7

pow(10, 7) is 10,000,000 (I've put commas in for clarity). pow(10, 7)10,000,000 (为清楚起见,我在其中加了逗号)。

num2 / sub % 10; is evaluated as (num2 / sub) % 10 due to the precedence of / and % being the same so evaluation is from left to right. 由于/%优先级相同,所以将其评估为(num2 / sub) % 10 ,因此评估是从左到右。

This is (1,234,567 / 10,000,000) % 10 which is 0 % 10 since integer division truncates the number. 这是(1,234,567 / 10,000,000) % 10 ,它是0 % 10因为整数除法会截断该数字。 So your first output is 0 . 因此,您的第一个输出是0

The simplest fix is probably to subtract 1 from the initial value of length . 最简单的解决方法可能是从length的初始值中减去1。

Try this code. 试试这个代码。 Now you will find your desire output 现在您将找到您想要的输出

public static void main(String[] args){

    int num = 0;
    int eachNum = 0;
    Scanner input = new Scanner(System.in);

    System.out.print("Enter your number here: ");
    num = input.nextInt();

    int num2 =  num;
    int length = String.valueOf(num).length()-1;
    while ( length >= 0 ) {

        int substraction =(int) Math.pow(10, length);
        int sub = (int) substraction; 
            eachNum = (num2 / sub) % 10;
            System.out.print(eachNum + "\t");
            length--;

    }

}//end main

You should also skip case when num2 < substraction. 当num2 <减时,还应该跳过大小写。 I try changing while in this way: 我尝试以这种方式进行更改:

while ( length >= 0 ) {
    int substraction =(int) Math.pow(10, length--);
    int sub = (int) substraction; 

    if (num2 < substraction)
        continue;

    eachNum = num2 / sub % 10;
    System.out.print(eachNum + "\t");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在ArrayList中而不是整数值中得到此结果? - Why am I getting this results in my ArrayList instead of the integer values? 为什么在将字符串转换为整数时会出现NumberFormatException? - Why am I getting NumberFormatException while converting a string to integer? 为什么我在使用最大 integer 值时得到荒谬的值? - Why I am getting absurd values on using maximum integer value? 为什么通过在泛型中将Integer类型参数设置为Integer变量而出错? - Why i am getting error by setting the Integer type parameter to the Integer variable in generics? 为什么我会收到“整数太大”错误? 我在最后加上一个“L” - Why am I getting the error "integer number too large"? I'm including an "L" at the end 我得到了意外的整数值 - I am getting an unexpected integer value 我在Java 8代码列表中将Integer值获取为&#39;Integer&#39; - I am getting Integer value as 'Integer' in the list in my Java 8 code 为什么会收到NoSuchElementException? - Why am I getting a NoSuchElementException? 为什么我得到nullPointerException - why am I getting nullPointerException 为什么我得到ArrayIndexOutOfBoundsException - Why am i getting ArrayIndexOutOfBoundsException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM