Netbeans 中奇怪的 System.out.print 行为

发布于 2024-08-07 19:10:06 字数 1056 浏览 3 评论 0原文

我从下面代码片段中 Scanner 声明正下方的 System.out.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());
    }
}

I'm getting weird results from the System.out.print immediately below the Scanner declaration in the code snippet below. It seems like it's executing twice. I've debugged it and immediately after executing the print statement I get this in standard out:

run:
Input a freaking binary number: Input a freaking binary number:

I added the "freaking" to verify it wasn't somehow entering the while loop print without me knowing.

For your info this is being executed in the netbeans IDE 6.7.1 on a 64 bit vista machine with the 64 bit JDK. Hopefully you can see the error of my ways!

Thanks!

Edit: When executing the Netbeans generated JAR file on the command line the statement only prints once. Has anyone encountered this kind of weird behavior in Netbeans that might know how I can prevent this from happening. I hate having to work outside my IDE during development cycles.

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());
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2024-08-14 19:10:06

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

在这一行中:

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

删除第二个尾随空格。

The "2 space after :" in print(String s) bug in Netbeans?

In the line:

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

delete the 2nd trailing space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文