java Java中的反转整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29933436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:08:36  来源:igfitidea点击:

Reverse integers in Java

javaintegerreverse

提问by Eternal Vapor

Below is a code that I have for flipping a given integer and displaying the flipped results. It runs but I have issues for when the number is smaller than two digits. It obviously cannot be flipped. I wanted to make the loop an if else stating "if number is two digits or more reverse." "Else state that the integer needs to be two or more digits." how could I go about this?

下面是我用于翻转给定整数并显示翻转结果的代码。它运行但是当数字小于两位数时我有问题。显然不能翻转。我想让循环成为一个 if else 说明“如果数字是两位数或更多位倒数”。“否则说明整数需要是两位或更多位数字。” 我怎么能去呢?

import java.util.Scanner;

public class ReverseInteger {

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

        System.out.print("Enter an integer that you would like to have reversed: ");
        int number = input.nextInt();

        reverse(number);
    }

    public static void reverse(int userInteger)
    {
        int tempDigit = 0;

        while (userInteger > 0){

            tempDigit = userInteger % 10;
            System.out.print(tempDigit);
            userInteger = userInteger / 10;
        }
    }
}

I am trying to get it to understand that 01 can be converted to 10. This would need to be done by the code understanding that the userInteger is more than one digit but I cant seem to figure out how to do that... Any ideas on how I can get this to check for two digits and execute the loop accordingly would be appreciated!

我试图让它理解 01 可以转换为 10。这需要通过理解 userInteger 超过一位的代码来完成,但我似乎无法弄清楚如何做到这一点......任何想法关于如何让它检查两位数并相应地执行循环将不胜感激!

回答by Czipperz

I recommend using String.valueof(int).toCharArray();and looping through in reverse to compose a new char[]. Then use Integer.parseInt(String);

我建议使用String.valueof(int).toCharArray();并反向循环来组合一个new char[]. 然后使用Integer.parseInt(String);

回答by Vincent Rodomista

public static void reverse(int n)
{
    int temp = 0;
    int count = 0;

    while(n != 0)
    {
        if(n%10 == 0)count++;
        temp = temp*10 + n %10;
        n /= 10;
    }
    for(int i = 0; i < count; i++)
    {
        System.out.print(0);
    }
    System.out.println(temp);
}

回答by Rezwan Azfar Haleem

Convert the int to a String using Integer.toStringmethod and save it to a string. Declare a String that will be later used as output. Start a for loop that goes through the number ( which was converted to a String) from its end to its beginning. It add each character from end to start to the output String. This results in the output String to be the reverse of the number String. Then just simply convert the output String using Integer.parseIntmethod and return the intvalue.

使用Integer.toString方法将 int 转换为 String并将其保存为字符串。声明一个稍后将用作输出的字符串。启动一个 for 循环,从数字的末尾到开头遍历数字(已转换为字符串)。它将从结束到开始的每个字符添加到输出字符串。这导致输出字符串与数字字符串相反。然后只需简单地使用Integer.parseInt方法转换输出字符串并返回int值。

The code should look like:

代码应如下所示:

public static int reverse(int n)
{
    String number = Integer.toString(n);
    String output;
    for(int i = number.length()-1; i >= 0; i--)
         output += number.charAt(i);
    return Integer.parseInt(output);
}