循环前的无限缓冲区?

时间:2018-03-01 14:46:20

标签: java recursion while-loop

简单的代码重复一个整数只是反转它。必须通过按Enter来打破循环,但是当我尝试运行此代码时它只是无限缓冲,并且没有显示任何内容?

import java.util.*;
public class ReverseDisplay
{
    public static void main(String Args[])
    {
        Scanner kb = new Scanner(System.in);
        boolean repeat = true;
        while (repeat == true);
        {
            System.out.print("Enter an integer to be reversed or hit enter to end program:");
            int line = kb.nextInt();
            int length = (int)(Math.log10(line) + 1);
            if (length == 1)
            {
                repeat = false; 
            }
            else
            {
                System.out.print("The reverse of " + line + " is ");
                reverseDisplay(line);
            }
        }

    }
    public static void reverseDisplay(int value)
    {   
        if (value < 10)
        {
            System.out.println(value);
            return;
        }
        else
        {
            System.out.print(value % 10);
            reverseDisplay(value / 10);
        }
    }   
}

编辑:删除分号导致它正常运行,感谢您的快速响应。

1 个答案:

答案 0 :(得分:2)

应该没有&#34;;&#34; &#34; while&#34;。你的代码在无限循环中有效地运行,绝对没有任何东西。