使用扫描仪时无限循环?

发布于 2024-10-14 08:42:28 字数 248 浏览 7 评论 0原文

boolean z = false;
do {
    try {
        a = sc.nextInt();
        z = true;
    }
    catch(Exception e) {
    }
}
while(!z);

试试这个。如果您第一次尝试使用整数,它会正确执行。但是,如果您输入错误的文本类型,即使您接下来输入 int 并跳过将布尔值分配给 true,它也会变成无限循环。这是为什么呢?

boolean z = false;
do {
    try {
        a = sc.nextInt();
        z = true;
    }
    catch(Exception e) {
    }
}
while(!z);

Try this. If you try an integer the first time it executes properly. However if you enter the wrong type of text it turns into an infinite loop even if you enter an int next and skips assigning the boolean value to true. Why is this?

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

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

发布评论

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

评论(1

甜是你 2024-10-21 08:42:28

您的问题是由于未处理行尾标记,因此扫描仪处于挂起状态。你想做这样的事情:

import java.util.Scanner;

public class Foo2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = 0;
        boolean catcher = false;
        do {
            try {
                System.out.print("Enter a number: ");
                a = sc.nextInt();
                catcher = true;
            } catch (Exception e) {
            } finally {
                sc.nextLine();
            }

        }
        // !!while(catcher == false);
        while (!catcher);

        System.out.println("a is: " + a);
    }
}

另外, while (z == false) 是不好的形式。使用 while (!z) 会更好。这可以防止 while (z = false) 错误,并且是表达此错误的更简洁的方式。

马塞洛的编辑:

马塞洛,感谢您的意见和建议!您是说下面 if 块中的条件不会更改布尔值 spam 的值吗?

  boolean spam = true;

  if (spam = false) {
     System.out.println("spam = false");
  }

  System.out.printf("spam = %b%n", spam);

因为如果它确实改变了它,如果编码员打算编写 if (spam == false),那么他们不会期望这种情况,那么这可能会产生微妙且危险的副作用。再次感谢您帮助我澄清这一点!

Your problem is from not handling the end of line token and so the scanner is left hanging. You want to do something like so:

import java.util.Scanner;

public class Foo2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = 0;
        boolean catcher = false;
        do {
            try {
                System.out.print("Enter a number: ");
                a = sc.nextInt();
                catcher = true;
            } catch (Exception e) {
            } finally {
                sc.nextLine();
            }

        }
        // !!while(catcher == false);
        while (!catcher);

        System.out.println("a is: " + a);
    }
}

Also, while (z == false) is bad form. You're much better off with while (!z). This prevents the while (z = false) error, and is a cleaner way of expressing this.

edit for Marcelo:

Marcelo, thanks for your input and advice! Are you saying that the conditional in the if block below will not change the value of the boolean, spam?

  boolean spam = true;

  if (spam = false) {
     System.out.println("spam = false");
  }

  System.out.printf("spam = %b%n", spam);

Because if it does change it, the coder wouldn't expect this if they intended to write if (spam == false), then there could be subtle and dangerous side effects from this. Again, thanks for helping to clarify this for me!

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