这个相反的订单我错过了什么?

时间:2016-12-05 23:14:16

标签: java reverse

这是我到目前为止所做的:

   import java.util.Scanner;

   public class ReverseNumber {

      public static void main(String args[])
      {
         int n, reverse = 0;

         System.out.println("Enter an integer: ");
         Scanner in = new Scanner(System.in);
         n = in.nextInt();

         while( n != 0 )
         {
             reverse = reverse * 10;
             reverse = reverse + n%10;
             n = n/10;
         }

         System.out.println(" in reverse order is "+reverse);
      }
   }

如果用户输入为123,那么它打印的内容,现在的样式是:

输入一个整数:

以相反的顺序是321

我需要知道要把它放在'前面的地方,以相反的顺序打印123是'言。

3 个答案:

答案 0 :(得分:1)

在更改输入值时,需要将其存储在不同的变量中并更改Sys out语句以添加实际值,例如:

.toggle()

答案 1 :(得分:1)

我会这样做

 public static void main(String[] args) {
                int n, reverse = 0;
                System.out.println("Enter an integer: ");
                Scanner in = new Scanner(System.in);
                n = in.nextInt();
                int number = n;
                System.out.println(n);
                while( n != 0 )
                {
                    reverse = reverse * 10;
                    reverse = reverse + n%10;
                    n = n/10;
                }
                System.out.println(number+" in reverse order is "+reverse);
        }

答案 2 :(得分:1)

如果要同时初始化多个变量,请使用两个不同的行。

由于您要更改n,请创建一个可以使用的副本。

import java.util.Scanner;

class Main {
  public static void main(String args[])
  {
     int n, reverse, initialNumber;
     n = reverse = initialNumber = 0;

     System.out.print("Enter an integer:");
     Scanner in = new Scanner(System.in);
     n = in.nextInt();
     initialNumber = n;

     while( n != 0 )
     {
         reverse = reverse * 10;
         reverse = reverse + n%10;
         n = n/10;
     }

     System.out.println(initialNumber + " in reverse order is " + reverse);
  }
}

试试here!