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

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

提问人:Limbo 提问时间:2/4/2022 更新时间:2/4/2022 访问量:141

问:

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

程序代码:

  //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

怎么了?

Java 循环 if-statement 整数 java.util.scanner

评论

1赞 MadProgrammer 2/4/2022
难道你不需要一个循环来继续计算数字反转,直到你(或其他什么?以 Java 程序反转数字为例0
0赞 sorifiend 2/4/2022
如果你只需要对两个字符执行此操作,那么通过将 int 转换为字符串,然后像这样使用 charAt(x) 来获得更简单的解决方案:否则,您应该使用上面建议的循环。String converted = num + ""; System.out.println("Reversed Number: " + converted.charAt(1) + converted.charAt(0));
0赞 VAIBHAV NIRMAL 2/4/2022
你的代码是对的,你只是忘记在那里写一个循环。.请参阅我在下面发布的解决方案。

答:

1赞 Zag 2/4/2022 #1

你的问题就在这里

   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;
    }
0赞 VAIBHAV NIRMAL 2/4/2022 #2

你的问题解决了!!你忘了在那里写 while 循环

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

这个 while 循环每次将数字除以 10,并在每个循环中将数字添加到反向数字中,而数字将等于 0,则循环将中断

现在这是您的整个工作代码:

   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
0赞 Jagadeesh mano 2/4/2022 #3

这个逻辑必须遍历输入数字的每个数字,所以试着把它加到循环中

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

}

0赞 Cheng Thao 2/4/2022 #4

如果是数字,则 n%10 为您提供正确的数字,并为您提供左数字。nn/10

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

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

输出:

31
13