简体   繁体   English

Java整数数字阅读器

[英]Java Integer digit reader

I am creating a method that needs to read a 5 digit integer digit by digit. 我正在创建一种方法,需要逐位读取5位整数。 (ie 26505 would be read as 2 6 5 0 5, and each digit could be read individually in another method) I cannot convert the integer to a string and read each character because I need the digits to be read by another method. (即26505将被读取为2 6 5 0 5,并且每个数字都可以通过另一种方法单独读取)我无法将整数转换为字符串并读取每个字符,因为我需要通过另一种方法来读取数字。 It has also been suggested to use %10 but that wouldnt give me individual digits. 还建议使用%10,但这不会给我单独的数字。 Also, the integer needs to be read digit by digit from the left to right. 同样,整数需要从左到右逐位读取。 I hope this is clear enough, but I am really confused on how to complete this and everything I have tried does not work. 我希望这已经足够清楚了,但是我对如何完成此工作感到非常困惑,而我尝试过的所有方法都不起作用。 Any help offered would be appreciated, thank you. 任何提供的帮助将不胜感激,谢谢。

    while(d>=10){  
    j=code%d;
    d=d/10;
    printDigit(j)

For getting single digits from left-to-right : - 为了从左到右获得一位数字:-

26505 / 10000 = 2
26505 % 10000 = 6505

6505 / 1000 = 6
6505 % 1000 = 505

505 / 100 = 5
505 % 100 = 5

5 / 10 = 5 

I think you can now implement it. 我认为您现在可以实施它。

But, if you are OK with traversing from right-to-left , it would be easier, since then your denominator will be fixed to 10: - 但是,如果您可以从右向左遍历就可以了,因为这样您的分母将固定为10:

26505 % 10 = 5
26505 / 10 = 2650

2650 % 10 = 0
2650 / 10 = 265

265 % 10 = 5
265 / 10 = 26

26 % 10 = 6
26 / 10 = 2

2 % 10 = 2 

This will return an array with the integer digits in order. 这将按顺序返回一个具有整数位的数组。

public static int[] integerToDigits(int n)
{
    int[] digits= new int[5];
    int temp = n;
    for(int i = 0; i < 5; i++)
    {
        digits[4-i] = temp % 10;
        temp /= 10;
    }
    return digits;
}

integerToDigits(12345) = {1,2,3,4,5}

Something totally lazy would look like 完全懒惰的样子

final int [] digits = {score % 1000 / 100, score % 100 / 10, score % 10};

which would work if you know your ints will never be larger than 999, for example. 例如,如果您知道自己的整数永远不会大于999,这将起作用。

From this example, it is trivial to improve it to support arbitrary integers by calculating the next power of ten from your integer and then adding digits to the array in a loop. 在此示例中,通过计算整数的下一个10的幂次然后将数字加到循环中来改进它以支持任意整数是微不足道的。

You can do this: 你可以这样做:

  1. you can create a integer array and parse the each character of the user input string into and store into an array 您可以创建一个整数数组并将用户输入字符串的每个字符解析为并存储到数组中
  2. then you can use this array for your other method. 那么您可以将此数组用于其他方法。

eg 例如

Scanner scan = new Scanner(System.in);
    String str = scan.nextLine();
    int[] digitArray = new int[str.length()];
    for(int i=0; i<str.length(); i++)
    {
        String temp = Character.toString(str.charAt(i));
        System.out.println("temp is "+temp);
        digitArray[i] = Integer.parseInt(temp);
    }
    for (int i =0; i<digitArray.length; i++) {
        System.out.println(digitArray[i]);
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM