使用for循环java反向制作数字

时间:2018-11-28 03:28:00

标签: java

因此,我试图使用forloop反转Java中的数字,我得到了正确的值,但是我不确定这是否是最好的方法。

package forloops;
/*
% prints the last number in the sequence
/ prints every number except for the last one
*/
public class modulusForLoops {
public static void main(String[]args) {
    int orig = 123456789;
    int num = orig;
    for (int i = 0; i < 1; i++) {
        num = orig % 10; //9
        int secondDigit = orig / 10; //12345678
        int secondDigitPrinted = secondDigit % 10; //8
        int thirdDigit = secondDigit / 10; //1234567
        int thirdDigitPrinted = thirdDigit % 10; //7
        int fourthDigit = thirdDigit / 10; //123456
        int fourthDigitPrinted = fourthDigit % 10; //6
        int fifthDigit = fourthDigit / 10; //12345
        int fifthDigitPrinted = fifthDigit % 10;
        int sixthDigit = fifthDigit / 10; //1234
        int sixthDigitPrinted = sixthDigit % 10; //4
        int seventhDigit = sixthDigit / 10; //123
        int seventhDigitPrinted = seventhDigit % 10; //3
        int eigthDigit = seventhDigit / 10; //12
        int eigthDigitPrinted = eigthDigit % 10; //2
        int lastDigit = eigthDigit / 10; //1
        System.out.println(orig + " reversed is " + num + secondDigitPrinted + thirdDigitPrinted + fourthDigitPrinted + fifthDigitPrinted + sixthDigitPrinted + seventhDigitPrinted + eigthDigitPrinted + lastDigit);
    }
}
}

3 个答案:

答案 0 :(得分:1)

您可以简单地将其转换为String并使用java.lang.StringBuilder反转字符串。

int orig = 123456789;
String numString = Integer.toString(orig);
String reversed = "";
for (int i = numString.length() - 1; i >= 0; i--) { // loop through the string from back to front
    reversed += numString.charAt(i); // add each character to the resulting string
}
System.out.println(reversed);

或者

int orig = 123456789;
String numString = Integer.toString(orig); // convert int to String
String reversed = new StringBuilder(numString).reverse().toString(); // reverse string
System.out.println(reversed);

答案 1 :(得分:0)

以防万一,您想知道如何通过模数和循环来做到这一点。想法是从源弹出单位数字,并在每次迭代中以数字方式将其推到目标。

    int orig = 123456789;   //assume > 0
    int num = 0;
    for(int temp = orig;temp > 0;temp/=10)
    {
        num = num * 10 + temp % 10;
    }
    System.out.println(orig + " reversed is " + num);

答案 2 :(得分:0)

让我们坚持要反转任何数字的逻辑。为了更好地理解,让我们列出您正在使用的算法步骤。

Repeat below steps until there are no digits left in the given number:
    I) get the last digit from the number, i.e. lastDigit = number % 10
    II) remove the last digit from the number, i.e. numberWithoutLast = number / 10

当我们要多次执行一系列步骤(即重复执行这些步骤)时,我们会使用for,while或do ... while等循环结构

因此,如果我们要重写您的程序(循环部分),它将如下所示:

public static void main(String[] ar) {
    int orig = 123456789;
    int lastDigit = 0;
    /* we'll use the copy of original number for step I & II
     * instead of messing with the original number
     */
    int numberWithoutLast = orig;
    String reversed = ""; // we'll use this to store every last digit
    for(int i = 0;
        i < Integer.toString(orig).length(); /* this will repeat the loop for number of digits in "orig" */
        i++) {
        lastDigit = numberWithoutLast % 10;
        reversed += Integer.toString(lastDigit);
        numberWithoutLast = numberWithoutLast / 10;
    }
    // lastly we print the reversed number
    System.out.println("Reversed Number: " + reversed);
}

这是逆整数的手动方法。对于自动方式,您可以查看@Andreas的答案。