3

我从下面代码片段中 Scanner 声明下方的 System.out.print 得到奇怪的结果。它似乎执行了两次。我已经对其进行了调试,并在执行 print 语句后立即得到了标准输出:

运行:输入一个奇怪的二进制数:输入一个奇怪的二进制数:

我添加了“吓人”来验证它没有在我不知情的情况下以某种方式进入 while 循环打印。

为了您的信息,这是在带有 64 位 JDK 的 64 位 vista 机器上的 netbeans IDE 6.7.1 中执行的。希望你能看到我的方式的错误!

谢谢!

编辑:在命令行上执行 Netbeans 生成的 JAR 文件时,该语句只打印一次。有没有人在 Netbeans 中遇到过这种奇怪的行为,可能知道我可以如何防止这种情况发生。我讨厌在开发周期中不得不在我的 IDE 之外工作。

private void getInput()
{
    Scanner scanner = new Scanner(System.in);

    System.out.print("Input a freaking binary number:  ");

    // Grab the next inputed long and save it in the currentValueInBinary
    // member variable
    setCurrentValueInBinary(scanner.nextLong());

    // Loop until a valid binary number is retrieved
    while (!isNumberBinary(currentValueInBinary))
    {   // Input was negative, report error and re-request input
        System.out.println("Input must be a Binary value");
        System.out.print("\nInput a binary number:  ");

        setCurrentValueInBinary(scanner.nextLong());
    }
}
4

1 回答 1

3

Netbeans 中的 print(String s) 错误中的“2 个空格:”?

在行中:

System.out.print("输入一个奇怪的二进制数:");

删除第二个尾随空格。

于 2009-10-16T14:56:00.170 回答