Java 程序不会显示整个输出 - 输入整数的倒数

Java program won't show the entire output - the reverse of an inputted integer

我有一个程序,用于获取用户的输入(两位整数)并输出它的相反值。虽然编译没有错误,但它只输出第一个整数而不输出第二个。

程序代码:

  //Set variable
  int reversed = 0;
  int requiredLength = 2;
  
  //Get User Input
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter a two-digit number:");

  while (sc.hasNext()) {
     //Conditional for User Input
     if (sc.hasNextInt()) {
        //Set scanner variables
        int num = sc.nextInt();
        int numLength = String.valueOf(num).length();
        if (numLength == requiredLength) {
           //Reverse integer
           int digit = num % 10;
           reversed = reversed * 10 + digit;
           num /= 10;
           System.out.println("Reversed Number: " + reversed);
           break;
        } else {
           //Error message
           System.out.println("Error: Input integer with two-digits");
           System.out.print("Please, enter a two-digit number:\n");
        }//End of conditional
     } else {
        //Error message
        System.out.println("Error: Input not an integer");
        System.out.print("Please, enter a two-digit number:\n");
        sc.next();
     }//End of conditional
  }//End of while loop

首选输出:

Please enter a two-digit number:
45
Reversed: 54

实际产量

Enter a two-digit number:
45
Reversed: 5

怎么了?

你的问题就在这里

   if (numLength == requiredLength) {
       //Reverse integer
       int digit = num % 10;
       reversed = reversed * 10 + digit; 
       num /= 10;
       System.out.println("Reversed Number: " + reversed);
       break;
    }

我怀疑您是想将其包含在数字循环中,然后对输入循环感到困惑。因为你知道它只是两位数,所以你可以这样做。

   if (numLength == requiredLength) {
       //Reverse integer
       int digit = num % 10;
       int reversed = 10 * digit + num / 10;
       System.out.println("Reversed Number: " + reversed);
       break;
    }

您的问题已解决!! 你忘了写 while loop 那里:

 while (num!=0){
         int digit = num % 10;
         reversed = reversed * 10 + digit;
         num /= 10;
       }

这个while循环每次都把number除以10,每次循环都把取反的number加digit,while number等于0循环就会break

现在这是您的全部工作代码:

   int reversed = 0;
        int requiredLength = 2;

        //Get User Input
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a two-digit number:");

        while (sc.hasNext()) {
            //Conditional for User Input
            if (sc.hasNextInt()) {
                //Set scanner variables
                int num = sc.nextInt();
                int numLength = String.valueOf(num).length();
                if (numLength == requiredLength) {
                    //Reverse integer
                   while (num!=0){
                       int digit = num % 10;
                       reversed = reversed * 10 + digit;
                       num /= 10;
                   }
                    System.out.println("Reversed Number: " + reversed);
                    break;
                } else {
                    //Error message
                    System.out.println("Error: Input integer with two-digits");
                    System.out.print("Please, enter a two-digit number:\n");
                }//End of conditional
            } else {
                //Error message
                System.out.println("Error: Input not an integer");
                System.out.print("Please, enter a two-digit number:\n");
                sc.next();
            }//End of conditional
        }//End of while loop

此逻辑必须遍历输入数字的每一位,因此尝试将其添加到循环中

while(number != 0){
mod = number % 10;
number = number / 10;
result = result * 10 + mod;

}

如果 n 是数字,那么 n%10 会给出正确的数字,而 n/10 会给出左边的数字。

要将它们组合成一个数字,您需要将右边的数字乘以 10 向左移动。然后从左边开始添加数字。

int n = 31;
if (n/10 < 10){
  System.out.println(n%10*10 + n/10);
}

输出:

31
13