【问题标题】:Test a condition in a while loop then exit while loop in Java在 while 循环中测试条件,然后在 Java 中退出 while 循环
【发布时间】:2017-02-25 02:13:18
【问题描述】:

我是 Java 新手,我正在尝试了解如何在 for 循环中嵌套 if 语句,并在执行 if 语句后让它退出 for 循环。我有一个数组,for循环遍历数组以查看一个ID是否存在,如果它应该删除它,如果它不存在那么它应该打印一条错误消息。发生的情况是在 while 循环中的嵌套 if 语句中测试条件并打印 3 次错误消息。我希望它只打印一次错误消息。

在我的主要方法中,我有

remove("3");
remove("3");

在第一个上,它应该只删除该 ID 并打印它是 rem,第二个它应该只打印一次错误消息。这是一个学校项目,不需要用户输入。我只是想了解如何在不打印重复错误消息的情况下完成这项工作

public static void remove(String studentID) 
{

    for (int i = 0; i < thestudents.size(); i++) 
    {

        Student temp = thestudents.get(i);

        if (temp.getStudentID()==(Integer.parseInt(studentID))) 
        {
            thestudents.remove(i);
            System.out.println("Student " + temp.getFirstName() + " was removed");
        }
        else
        {
            System.out.println("Student with ID " + studentID + " Was not found!");
        }
    }
}

结果:

未找到 ID 为 3 的学生! 未找到 ID 为 3 的学生! 学生杰克被删除 未找到 ID 为 3 的学生! 未找到 ID 为 3 的学生! 未找到 ID 为 3 的学生! 未找到 ID 为 3 的学生! 未找到 ID 为 3 的学生!

期望:

学生杰克被删除 未找到 ID 为 3 的学生!

【问题讨论】:

    标签: java if-statement while-loop


    【解决方案1】:

    只加一个break会删除匹配后的输出,但会保留匹配前的输出。

    我猜你想摆脱所有的假阴性输出。

    因此,您必须在循环之后移动负输出(即else 块的内容)(删除else 行),并确保在找到ID 时不会执行此代码。

    最好的方法是添加一个return 作为if 块中的最后一条语句。

    for (int i = 0; i < thestudents.size(); i++) 
    {
        Student temp = thestudents.get(i);
        if (temp.getStudentID()==(Integer.parseInt(studentID))) 
        {
            thestudents.remove(i);
            System.out.println("Student " + temp.getFirstName() + " was removed");
            return; // leaving the method whe ID found
        }
    }
    // is only executed when ID not found
    System.out.println("Student with ID " + studentID + " Was not found!)";
    

    【讨论】:

    • 谢谢!这行得通。我之前使用了 return 但没有将错误消息移到循环之外。再次感谢您!
    【解决方案2】:

    只需在if 语句中添加break。如果if 语句是true,则循环将终止。

    if (temp.getStudentID()==(Integer.parseInt(studentID))) {
        hestudents.remove(i);
        System.out.println("Student " + temp.getFirstName() + " was removed");
        break;
    }
    

    【讨论】:

    • 没错!与回报相比,休息是更好的选择;
    • break 会比 return 更好 不,不是! break 在循环之后需要额外的逻辑来确定循环是在没有命中的情况下完成还是在命中的情况下提前离开。 return 不需要这个额外的逻辑。
    【解决方案3】:

    您可以使用break 语句来终止循环,或者更好的是,在找到合适的项目后使用return 语句来完全终止该方法:

    public static void remove(String studentID) 
    {
    
        for (int i = 0; i < thestudents.size(); i++) 
        {
    
            Student temp = thestudents.get(i);
    
            if (temp.getStudentID()==(Integer.parseInt(studentID))) 
            {
                thestudents.remove(i);
                System.out.println("Student " + temp.getFirstName() + " was removed");
                return;
            }
        }
    
        // If we get here it means we haven't returned, so the student wasn't found
        System.out.println("Student with ID " + studentID + " Was not found!");
    }
    

    【讨论】:

    • 那么 return 将停止执行,但是,它也会终止该方法。所以最后一条语句不会打印任何东西。另外,如果它本来是一个int类型的方法,那么,你不能简单地对'return;'
    • @RishabhKumar 我们希望最后一条语句不打印,因为确实找到了学生。如果它不是 void 方法,您只需返回一个值。
    猜你喜欢
    • 2017-09-29
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 2011-12-18
    • 2013-03-01
    • 2022-09-23
    • 2013-05-15
    相关资源
    最近更新 更多