将字符串数转换为整数数组

时间:2022-02-02 21:46:17

I want to read a number as a String, and split its characters to an integer array, and find the sum of it's digits by looping through that integers array.

我想读取一个数字作为字符串,并将其字符拆分为整数数组,并通过循环遍历整数数组找到它的数字之和。

This is my code so far:

到目前为止这是我的代码:

public static void main(String[] args) {
    Scanner S = new Scanner(System.in);
    String Number = S.next();
    int counterEnd = Number.length();
    int sum = 0 ;

    for ( int i = 0 ; i < counterEnd ; i++) {
         sum += sum + (Number.charAt(i));
    }

    System.out.println(sum);
}

Unfortunately, this code prints the sum of ASCII not the digits.

不幸的是,此代码打印ASCII的总和而不是数字。

4 个解决方案

#1


5  

You can subtract the '0' character (i.e. '1' - '0' is 49 - 48 = 1):

你可以减去'0'字符(即'1' - '0'是49 - 48 = 1):

sum += Number.charAt(i) - '0';

#2


4  

You could, has @August suggested, substract the character '0' to obtain the numeric value of the character (I find this approach kind of hackish). Or you can use Character.getNumericValue to achieve this:

@August建议你可以减去字符'0'以获得字符的数值(我发现这种方法有点hackish)。或者您可以使用Character.getNumericValue来实现此目的:

sum += Character.getNumericValue(Number.charAt(i)); //note it's sum += theDigit; not sum += sum + theDigit

You might also want to look at the enhanced for loop, as you basically don't need the index here:

您可能还想查看增强的for循环,因为您基本上不需要索引:

for(char c : Number.toCharArray()) {
    sum += Character.getNumericValue(c);
}

As of Java 8 you could also do it like this:

从Java 8开始,您也可以这样做:

int sum = Number.chars().map(Character::getNumericValue).sum();

It basically gets a Stream of the characters in the String, map each character to its corresponding numeric value and sum them.

它基本上获取String中字符的Stream,将每个字符映射到其对应的数值并对它们求和。

#3


0  

sum+= Integer.parseInt(String.valueOf(Number.charAt(i)));

#4


0  

You can do it like this:

你可以这样做:

   public static void main(String[] args) {
     Scanner S = new Scanner(System.in);
     String n = S.next();
     int sum = 0;

     for (int i = 0; i < n.length(); i++) {
       sum += Integer.parseInt(n.substring(i, i+1));
     }

     System.out.println(sum);
   }

Note: Replacing the body of the for loop with:

注意:用以下代码替换for循环的主体:

int offset = (n.substring(i, i+1).equals("-")) ? 1 : 0;
sum += Integer.parseInt(n.substring(i, i+1+offset));
i+=offset;

Will allow the program to take negative numbers. Ex: Inputting

将允许该程序采取负数。例如:输入

-24

Would return a positive 2.

会回归正面2。

#1


5  

You can subtract the '0' character (i.e. '1' - '0' is 49 - 48 = 1):

你可以减去'0'字符(即'1' - '0'是49 - 48 = 1):

sum += Number.charAt(i) - '0';

#2


4  

You could, has @August suggested, substract the character '0' to obtain the numeric value of the character (I find this approach kind of hackish). Or you can use Character.getNumericValue to achieve this:

@August建议你可以减去字符'0'以获得字符的数值(我发现这种方法有点hackish)。或者您可以使用Character.getNumericValue来实现此目的:

sum += Character.getNumericValue(Number.charAt(i)); //note it's sum += theDigit; not sum += sum + theDigit

You might also want to look at the enhanced for loop, as you basically don't need the index here:

您可能还想查看增强的for循环,因为您基本上不需要索引:

for(char c : Number.toCharArray()) {
    sum += Character.getNumericValue(c);
}

As of Java 8 you could also do it like this:

从Java 8开始,您也可以这样做:

int sum = Number.chars().map(Character::getNumericValue).sum();

It basically gets a Stream of the characters in the String, map each character to its corresponding numeric value and sum them.

它基本上获取String中字符的Stream,将每个字符映射到其对应的数值并对它们求和。

#3


0  

sum+= Integer.parseInt(String.valueOf(Number.charAt(i)));

#4


0  

You can do it like this:

你可以这样做:

   public static void main(String[] args) {
     Scanner S = new Scanner(System.in);
     String n = S.next();
     int sum = 0;

     for (int i = 0; i < n.length(); i++) {
       sum += Integer.parseInt(n.substring(i, i+1));
     }

     System.out.println(sum);
   }

Note: Replacing the body of the for loop with:

注意:用以下代码替换for循环的主体:

int offset = (n.substring(i, i+1).equals("-")) ? 1 : 0;
sum += Integer.parseInt(n.substring(i, i+1+offset));
i+=offset;

Will allow the program to take negative numbers. Ex: Inputting

将允许该程序采取负数。例如:输入

-24

Would return a positive 2.

会回归正面2。