我想知道为什么在被问及此方法中是否还有更多数字后键入“y”时,代码可以正常工作并离开循环,但是在键入“n”并按回车时,我需要键入“n”和再次点击返回或该过程只是挂起。
为什么?
字符串 'input' 是从 main 方法中的用户输入传递的,在本例中是“addition”。
private int add(String input)
    {
        int additionValue = 0;
        boolean keepGoing = true;
        if (input.matches("addition"))
        {
            while (keepGoing == true)
            {
                System.out.println("Next digit = (Type the digit)");
                additionValue = additionValue + scan.nextInt();
                System.out.println("Any more digits? Type y/n");
                if (scan.next().matches("Y|y"))
                {
                    keepGoing = true;
                }
                else if (scan.next().matches("N|n"))
                {
                    keepGoing = false;
                }
                else
                {
                    System.out.println("Great, you broke it.");
                    System.exit(1);
                }
            }
        }
    }
我已经设法通过使用使代码正常工作
System.out.println("Any more digits? Type y/n"); 
String yayOrNay = scan.next();
if (yayOrNay.length()==1 && yayOrNay.charAt(0)=='y')
                {
                    keepGoing = true;
                }
但这对我来说似乎有点太复杂了。