输入的所有奇数位数之和

huangapple go评论71阅读模式
英文:

The sum of all odd digits of an input

问题

System.out.println("输入一个数字:");
String input = in.nextLine();
int length = input.length();

int sum = 0;

for (int i = 0; i < length; i++) {
    int digit = Character.getNumericValue(input.charAt(i));  // 获取当前位置的数字
    if (digit % 2 == 1) {  // 判断数字是否为奇数
        sum += digit;  // 将奇数数字累加到总和
    }
}

System.out.println(sum);

注意:上述代码是对您提供的代码进行了修正,以实现计算输入中所有奇数数字的和。请在使用前确认您的变量和输入流的定义是否正确。

英文:

How to compute the sum of all 'odd digits of an input' using 'loop'. (For example, if the input is 32677, the sum would be 3 + 7 + 7 = 17.)

I can't quite figure out how to do that, can someone please help me out. This is what I have done so far, I don't know how to complete it or whether I have its right or wrong.

Any help would be appreciated!

System.out.println(&quot;Enter a number: &quot;);
String input = in.nextLine();
int length = input.length();

int sum = 0;
int digits = 0;

for (int i = 0; i &lt; length; i++) {
    if (length % 2 == 1) {
        digits += i;
        sum = digits++;
    }
}
System.out.println(sum);

答案1

得分: 3

以下是一个基于Java 8的解决方案:

final int result = input.chars()//从字符串创建一个字符流
                        .mapToObj(String::valueOf) //将每个字符转换为字符串,以便之后使用parseInt
                        .mapToInt(Integer::parseInt) //将字符转换为整数
                        .filter(i -> i % 2 == 1) //过滤掉偶数
                        .sum(); 
英文:

Here comes a Java8-based solution:

final int result = input.chars()//make a stream of chars from string
                        .mapToObj(String::valueOf) // make every character a String to be able to use parseInt later
                        .mapToInt(Integer::parseInt) // transform character in int
                        .filter(i -&gt; i % 2 == 1) // filter out even numbers
                        .sum(); 

答案2

得分: 1

你的输入不是很长时,不需要使用字符串。为了安全起见,使用 long 数据类型。
下面是带有注释的可工作代码(解释每个步骤)。

    long sumOddDigits(long value){
        long temp = value; // 将值复制到临时变量中
        long sum = 0;
        while(temp > 0){
          int digit = temp%10; // 获取数字的个位数。例如:227 的个位数是 7。
          temp = temp / 10; // 从数字中移除个位数。227 将变为 22。
          if(digit % 2 == 1){
            sum += digit;
          }
        }
        return sum;
    }
英文:

You don't need to use String if your input is not so long.
also for safe side use long datatype.
Here is the working code with comments (explain each step).

long sumOddDigits(long value){
    long temp = value; // copy in temp variable
    long sum = 0;
    while(temp &gt; 0){
      int digit = temp%10; // get last digit of number. example: 227 gives 7.
      temp = temp / 10; // remove that last digit from number.227 will be 22.
      if(digit % 2 == 1){
        sum += digit;
      }
    }
    return sum;
}

答案3

得分: 0

你对输入中的数字的解释不起作用。

System.out.println("输入一个数字:");
String input = in.nextLine();
int length = input.length();

int sum = 0;

for (int i = 0; i < length; i++) {
    int digit = input.charAt(i) - '0';
    if (digit % 2 == 1) {
        System.out.println("添加数字:" + digit);
        sum += digit;
    }
}
System.out.println(sum);
英文:

Your interpretation of the digits inside of input is not working this way.

System.out.println(&quot;Enter a number: &quot;);
String input = in.nextLine();
int length = input.length();

int sum = 0;

for (int i = 0; i &lt; length; i++) {
    int digit = input.charAt(i) - &#39;0&#39;;
    if (digit % 2 == 1) {
        System.out.println(&quot;Add digit: &quot; + digit);
        sum += digit;
    }
}
System.out.println(sum);

答案4

得分: 0

在循环中,使用 Integer.parseInt(input.charAt(i)) 来获取位置 i 处的数字。

if(length%2==1){ 这在这里没有意义。你想要检查数字是否为奇数,而不是字符串的长度。

英文:

In your loop, use Integer.parseInt(input.charAt(i)) to get the number at position i.

if(length%2==1){ that doesn't make sense here. You want to check if your number is odd, not the length of your string.

huangapple
  • 本文由 发表于 2020年9月11日 16:56:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63843846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定