Inputting a number then reversing it

*爱你&永不变心* 提交于 2020-01-04 06:28:28

问题


Ok so I wrote a program which asks user to input a number and then reverse it. I was successful in it however the program does not reverses numbers that end with a 0. for example if i enter 1234 it will print out 4321 however if i input 1200 it will only output 21. I tried converting the number that is to become output into string. Please help me understand where I am doing it wrong. Just remember I am a beginner at this :). Below is my code.

import java.util.*;
public class ReverseNumber
{
    public static void main (String [] args)
    {
        Scanner n = new Scanner(System.in);
        int num;
        System.out.println("Please enter the number");
        num = n.nextInt();
        int temp = 0;
        int reverse = 0;
        String str = "";
        System.out.println("The number before getting reversed " + num);
        while (num != 0)
        {
            temp = num % 10;
            reverse = reverse*10 + temp;
            num = num/10;
            str = Integer.toString(reverse);
        }
        //String str = Integer.toString(reverse);
        System.out.println("The reversed number is " + str);
    }
}

回答1:


You're storing your reversed number as an int. The reverse of 1200 is 0021, but that's just 21 as an int. You can fix it by converting each digit to a string separately.




回答2:


The problem is that you're calculating the reversed value as a number and, when it comes to numbers, there is no difference between 0021 and 21. What you want is to either print out the reversed value directly as you're reversing it or build it as a string and then print it out.

The former approach would go like this:

System.out.print("The reversed number is ");
while (num != 0)
{
    System.out.print(num % 10);
    num = num / 10;
}
System.out.println();

The latter approach would go like this:

String reverse = "";
while (num != 0)
{
    reverse = reverse + Integer.toString(reverse);
    num = num / 10;
}
System.out.println("The reversed number is " + reverse);

The latter approach is useful if you need to do further work with the reversed value. However, it's suboptimal for reasons that go beyond the scope of this question. You can get more information if you do research about when it's better to use StringBuilder instead of string concatenation.




回答3:


I actually found this way really interesting, as this is not how I usually would reverse it. Just thought to contribute another way you could reverse it, or in this case, reverse any String.

public static void main()
{
    Scanner n = new Scanner(System.in);
    System.out.print("Please enter the number:");
    int num = n.nextInt();
    System.out.println("The number before getting reversed is " + num);

    String sNum = Integer.toString(num);
    String sNumFinal = "";
    for(int i = sNum.length()-1; i >= 0; i--)
    {
        sNumFinal += sNum.charAt(i);
    }
    System.out.print("The reversed number is " + sNumFinal);
}

If you wanted to take this further, so that you can enter "00234" and have it output "43200" (because otherwise it would take off the leading zeros), you could do:

public static void main()
    {
        Scanner n = new Scanner(System.in);
        System.out.print("Please enter the number:");
        String num = n.next(); // Make it recieve a String instead of int--the only problem being that the user can enter characters and it will accept them.
        System.out.println("The number before getting reversed is " + num);

        //String sNum = Integer.toString(num);
        String sNumFinal = "";
        for(int i = num.length()-1; i >= 0; i--)
        {
            sNumFinal += num.charAt(i);
        }
        System.out.print("The reversed number is " + sNumFinal);
    }

And of course if you want it as an int, just do Integer.parseInt(sNumFinal);




回答4:


The reason the two zero is being stripped out is because of the declaration of temp and reverse variable as integer.

If you assigned a value to an integer with zero at left side, example, 000001 or 002, it will be stripped out and will became as in my example as 1 or 2.

So, in your example 1200 becomes something like this 0021 but because of your declaration of variable which is integer, it only becomes 21.




回答5:


 import java.util.Scanner;


 public class Reverse {
  public static void main(String args[]){

    int input,output=0;
    Scanner in=new Scanner(System.in);
    System.out.println("Enter a number for check.");
    input=in.nextInt();

    while (input!=0)
    {
    output=output*10;
    output=output+input%10;
    input=input/10;
    }
    System.out.println(output);
    in.close();
}

}



来源:https://stackoverflow.com/questions/11424294/inputting-a-number-then-reversing-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!