对于每个无限循环

时间:2015-04-04 23:48:16

标签: java foreach linked-list

我是一名初学程序员,我创建了一个方法,将用户输入字符串,检查它是一个整数还是一个字符,转换它然后将它传递给方法的下一部分每个循环检查存储在链表中的Person对象的实例的id值。

然而,它正在创建一个无限循环,我不知道如何打破它,以便它只输出一次。非常感谢任何帮助。

我只包含了我认为是问题的方法,如果你需要更多的程序部分,请告诉我,我会添加它。

public void theSiv() {
        System.out.println("Please enter client Id:");
        String s = In.nextLine();
        boolean isValidInteger = false;
        char choice = 'p';
        boolean exists = false;
        int searchid = 0;
        try
        {
            int i = Integer.parseInt(s);
            isValidInteger = true;
            searchid = i;
        }
        catch (NumberFormatException ex)
        {
            choice = s.charAt(0);
        }

        while (choice !='x'){
            for (Person b:clients)
                if  (b.getId() == searchid){
                    exists = true;
                    System.out.println("found client");}

            if(exists == false)
                System.out.println("  No such client");
        }

    }

1 个答案:

答案 0 :(得分:2)

无限循环是您的while (choice !='x'){,因为在choice循环的正文中没有任何内容会修改while。如果我理解您的代码,那么您可以更改为if

if (choice != 'x') {