简体   繁体   English

在没有循环的情况下将 integer 反转为 java

[英]reversing an integer in java without a loop

This is an homework problem这是一道作业题

Is there a way tor reverse a number in Java without using any loops?有没有办法在不使用任何循环的情况下反转 Java 中的数字? The only solution I can think of is reversing it using String and then casting it back to an integer.我能想到的唯一解决方案是使用 String 反转它,然后将其转换回 integer。

If you want to reverse a number withour using any loop you can use Recursion method call.如果您想在不使用任何循环的情况下反转数字,您可以使用递归方法调用。 Following program is doing same以下程序正在做同样的事情

public static void reverseMethod(int number) {
    if (number < 10) {
        System.out.println(number);
        return;
    } else {
        System.out.print(number % 10);
        reverseMethod(number / 10);
    }
}

public static void main(String args[]) {
    int num = 4567;
    reverseMethod(num);
}

Even if you were to reverse the number by casting it into a String, you would still need a loop if you want the program to work when having ints of different sizes.即使您通过将其转换为字符串来反转数字,如果您希望程序在具有不同大小的整数时工作,您仍然需要一个循环。 If I were to make a method to reverse a number but could not do it with loops, I would probably do it with recursion (which still uses loops indirectly).如果我要创建一个方法来反转一个数字但不能用循环来做,我可能会用递归来做(它仍然间接使用循环)。 The code will look something like this:代码如下所示:

class Main {
  public static void main(String[] args) {
    String input = "1234"; // or scanner to take in input can be implemented
    System.out.println(Integer.parseInt(reverseInt(input)));
  }
  public static String reverseInt(String x) {
    if (x.length() == 1) {
      return x;
    } else {
      return x.substring(x.length() - 1) + reverseInt(x.substring(0, x.length() - 1));
    }
   }


}

Hope this helps!希望这可以帮助!

By using reverse() of StringBuilder :通过使用StringBuilder reverse()

int number = 1234;
String str = String.valueOf(number);
StringBuilder builder = new StringBuilder(str);
builder.reverse();
number = Integer.parseInt(builder.toString());
System.out.println(number);

will print:将打印:

4321

if you want reverse method without loop and recursion then use this code如果您想要没有循环和递归的反向方法,请使用此代码

 int a=12345;
    int b,c,d,e,f;
    b=a%10;
    c=a%100/10;
    d=a%1000/100;
    e=a%10000/1000;
    f=a%100000/10000;
    System.out.println(b+","+c+","+d+","+e+","+f);

you can go like :你可以这样:

public int reverse(int x) {    
    
    String o = "";
    
    if (x < 0) {
        x *= -1;
                String s = Integer.toString(x);

        o += "-";
        o += new StringBuilder(s).reverse().toString();
    }
    else {
                String s = Integer.toString(x);

        o += new StringBuilder(s).reverse().toString();
    }
    try {
        int out = Integer.parseInt(o);
    
    //System.out.println(s);
    
        return out;        }
    catch (NumberFormatException e) {
        return 0;
    }
    
    
}

This is a solution using recursive method call这是使用递归方法调用的解决方案

public class Tester{公共课测试员{

public static int findReverse(int num, int temp){
    if(num==0){
        return temp;
    }else if(num<10){
        return temp*10 + num; //up to this is stopping condition
    }else{
        temp = temp*10 + num%10;
        return findReverse(num/10, temp);
    }
}

public static void main(String args[]){
    int num = 120021;
    int reverseNum = findReverse(num, 0);
    System.out.println(reverseNum);
    if(num == reverseNum)
        System.out.println(num +" is a palindrome!");
    else
        System.out.println(num +" is not a palindrome!");
}

} }

This will be fast.这会很快。

static int reverseNum(int num) {
    StringBuilder sb = new StringBuilder(String.valueOf(num));
    sb.reverse();
    return Integer.parseInt(sb.toString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM