简体   繁体   English

使用for循环反转Java中的整数

[英]Reversing an integer in Java using a for loop

This is a homework problem 这是一个作业问题

How would I reverse an integer in Java with a for loop? 如何使用for循环反转Java中的整数? The user will input the integer (I don't know how long it will be) and I need to reverse it. 用户将输入整数(我不知道它将是多长),我需要将其取反。 ie: If they enter 12345, my program returns 54321. 即:如果输入12345,我的程序将返回54321。

Here's the catch, you can't use String, StringBuffer, arrays, or other advanced structures in this problem. 这很重要,您不能在此问题中使用String,StringBuffer,数组或其他高级结构。

I have a basic idea of what I need to do. 我对需要做什么有基本的了解。 My problem is...in the for loop, wouldn't the condition need to be x < the length of the integer (number of digits)? 我的问题是...在for循环中,条件是否必须为x <整数的长度(数字位数)? How would I do that without String? 没有String我该怎么办?

Thanks for any input, and I'll add more information if requested. 感谢您的输入,如果需要,我将添加更多信息。

EDIT: 编辑:

Of course, after introspection, I realized I should use another for loop to do this. 当然,在内省之后,我意识到我应该使用另一个for循环来执行此操作。 What I did was create a for loop that will count the digits by dividing by 10: 我所做的是创建一个for循环,该循环将除以10来计数数字:

       int input = scan.nextInt();
        int n = input;
        int a = 0;

           for (int x = 0; n > 0; x++){
               n = n/10;
                a = a + 1;
            }

EDIT 2: 编辑2:

This is what I have 这就是我所拥有的

 int input = scan.nextInt();
        int n = input;
        int a = 0;
        int r = 0;
           for (int x = 0; n > 0; x++){
               n = n/10;
                a = a + 1;
            }
           for (int y = 0; y < n; y++) { 
                r = r + input%10;
                input = input/10;
            }
          System.out.println(input);

When I run it, it isn't reversing it, it's only giving me back the numbers. 当我运行它时,它并没有反转,只是给了我一些数字。 ie: if I put in 1234, it returns 1234. This doesn't make any sense to me, because I'm adding the last digit to of the input to r, so why wouldn't it be 4321? 即:如果我输入1234,它将返回1234。这对我来说没有任何意义,因为我要在r的输入中添加最后一位数字,所以为什么不输入4321?

While your original number is nonzero, take your result, multiply it by 10, and add the remainder from dividing the original by 10. 当原始数字为非零时,将结果乘以10,然后将原始数字除以10得到的余数相加。

For example, say your original number is 12345. Start with a result of 0. 例如,假设您的原始号码是12345。以0开头的结果。

  1. Multiply result by 10 and add 5, giving you 5. (original is now 1234.) 将结果乘以10并加5,得到5。(原始值为1234。)
  2. Multiply result by 10 and add 4, giving you 54. (original is now 123.) 将结果乘以10并加4,得到54。(原始值为123。)
  3. Multiply result by 10 and add 3, giving you 543. (original = 12.) 将结果乘以10并加3,得到543。(原始= 12)。
  4. Multiply result blah blah 5432. (original = 1.) 等等结果乘以5432。(原始=1。)
  5. Multiply, add, bam. 乘,加,ba。 54321. And 1 / 10, in int math, is zero. 54321。以int数学表示的1/10为零。 We're done. 大功告成

Your mission, should you choose to accept it, is to implement this in Java. 您的任务(如果您选择接受它)是用Java实现的。 :) (Hint: division and remainder are separate operations in Java. % is the remainder operator, and / is the division operator. Take the remainder separately, then divide the original by 10.) :)(提示:Java中的除法和余数是单独的操作。 %是余数运算符, /是除法运算符。将余数分开,然后将原始数除以10。)

You will need to use math to access each of the digits. 您将需要使用数学运算来访问每个数字。 Here's a few hints to get your started: 以下是一些入门的提示:

Use the % mod operator to extract the last digit of the number. 使用% mod运算符提取数字的最后一位。 Use the / division operator to remove the last digit of the number. 使用/除运算符删除数字的最后一位。 Stop your loop when you have no more digits in the number. 当数字中没有更多数字时,请停止循环。

This might not be the proper way but 这可能不是正确的方法,但是

   public static int reverseMe(int i){

    int output;
    String ri = i + "";

    char[] inputArray = ri.toCharArray();
    char[] outputArray = new char[inputArray.length];

    for(int m=0;m<inputArray.length;m++){
        outputArray[inputArray.length-m-1]=inputArray[m];
    }

    String result = new String(outputArray);
    output = Integer.parseInt(result);

    return output;
}
    public static void reverse2(int n){
    int a;


    for(int i = 0; i < n  ; i ++){
        a = n % 10;
        System.out.print(a);
        n = n / 10;


        if( n < 10){
            System.out.print(n);
            n = 0;
        }
        }

    }

here is the Answer With Correction of Your Code. 这是更正您的代码的答案。

import static java.lang.Math.pow;
import java.util.*;
public class MyClass {
public static void main(String args[]) {
    Scanner scan=new Scanner(System.in);
    int input = scan.nextInt();
    int n = input;
    int a = 0;
    int r = 0;
       for (; n > 0;){
           n = n/10;
            a = a + 1;
        }
       for (int y = 0; y < input;a--) { 
            r =(int)( r + input%10*pow(10,a-1));
            input = input/10;
        }
      System.out.println(r);
}
}

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

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