使用do-while循环反转一个数字

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

reversing a number using do-while

问题

import java.util.*;

public class Main {
    static int replace(int number) {
        if (number == 0)
            return 0;
        int digit = number % 10;

        if (digit == 0)
            digit = 1;

        return (number / 10) * 10 + digit;
    }

    static int Convert(int number) {
        if (number == 0)
            return 0;
        else
            return replace(number);
    }

    public static void main(String[] args) {
        int number;
        Scanner kb = new Scanner(System.in);

        System.out.print("Enter the number: ");
        number = kb.nextInt();
        System.out.println("replace:" + replace(number));
        int a, m = 0, sum = 0;

        do {
            a = replace(number) % 10;
            m = m * 10 + a;
            sum = sum + a;
            number = replace(number) / 10;
        } while (replace(number) > 0);

        System.out.println("Reverse:" + m);
        System.out.println("Sum of digits:" + sum);
    }
}
英文:

Create a java program that reads an integer number (NUM) and determine its reverse by using the division and remainder/modulo operators. If the last digit is zero, replace it with a one(1) before reversing the number. Output also the sum of all the digits.

import java.util.*;

public class Main {
    static int replace(int number){
        if (number == 0)
            return 0;
        int digit = number % 10;
      
        if (digit == 0)
            digit = 1;
    
        return (number/10) * 10 + digit;
    }
    
    static int Convert(int number){
        if (number == 0)
            return 0;
        else
        return replace(number);
    }
    
    public static void main(String[] args) {
        int number;
        Scanner kb = new Scanner(System.in);

        System.out.print("Enter the number : ");
        number = kb.nextInt();
        System.out.println("replace:"+replace(number));
        int a, m = 0, sum = 0;
    
        do{
            a = replace(number) % 10;
            m = m * 10 + a;
            sum = sum + a;
            number = replace(number) / 10;
        }
        while( replace(number) > 0);
    
        System.out.println("Reverse:"+m);
        System.out.println("Sum of digits:"+sum);
    }
}

Currently the problem occurs in reversing the number because it also replace the last digit of the number, this should not happen.

> Input/Output of current program
>
> Enter the number : 2300
>
> replace:2301
>
> Reverse:1132
>
> Sum of digits:7

答案1

得分: 1

import java.util.*;

public class Main {
    static int replace(int number){
        if (number % 10 == 0)
            return number + 1;
        return number;
    }

    static int Convert(int number){
        if (number == 0)
            return 0;
        else
            return replace(number);
    }

    public static void main(String[] args) {
        int number;
        Scanner kb = new Scanner(System.in);

        System.out.print("Enter the number: ");
        number = kb.nextInt();
        int a = 0, m = 0, sum = 0;

        number = replace(number);
        System.out.println("replace: " + number);

        do{
            a = number %  10;
            m = m * 10 + a;
            sum = sum + a;
            number /= 10;
        }
        while( number > 0);

        System.out.println("Reverse: " + m);
        System.out.println("Sum of digits: " + sum);
    }
}

Your code is fundamentally wrong because of the way you are replacing your numbers.

Changes made:

  1. Changed replacing algorithm (You cannot change all 0 values to 1 that is wrong and why you got the wrong values)
  2. Replace the number before you enter the loop. (You don't need to replace every iteration of the loop at 3 different places)

Expected output:

使用do-while循环反转一个数字


<details>
<summary>英文:</summary>

do this instead

    import java.util.*;
    
    public class Main {
        static int replace(int number){
            if (number %10 == 0)
                return number += 1;
            return number;
        }
    
        static int Convert(int number){
            if (number == 0)
                return 0;
            else
                return replace(number);
        }
    
        public static void main(String[] args) {
            int number;
            Scanner kb = new Scanner(System.in);
    
            System.out.print(&quot;Enter the number : &quot;);
            number = kb.nextInt();
            int a = 0, m = 0, sum = 0;
    
            number = replace(number);
            System.out.println(&quot;replace:&quot; + number);

            do{
                a = number %  10;
                m = m * 10 + a;
                sum = sum + a;
                number /= 10;
            }
            while( number &gt; 0);
    
            System.out.println(&quot;Reverse:&quot;+m);
            System.out.println(&quot;Sum of digits:&quot;+sum);
        }
    }

Your code is fundamentally wrong because of the way you are replacing your numbers.

Changes made:

1. Changed replacing algorithm (You cannot change all 0 values to 1 that is wrong and why you got the wrong values)
2. Replace the number before you enter the loop. (You don&#39;t need to replace every iteration of the loop at 3 different place)

Expected output:

[![output][1]][1]


  [1]: https://i.stack.imgur.com/CLmws.png

</details>



huangapple
  • 本文由 发表于 2020年5月5日 10:19:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61604646.html
匿名

发表评论

匿名网友

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

确定