为了账号安全,请及时绑定邮箱和手机立即绑定

如何在不返回值的情况下显示消息

如何在不返回值的情况下显示消息

梦里花落0921 2021-09-12 10:48:19
在这个函数中,如果 isEmpty ,我不想返回 0 或任何数值..我只想在我从 main 出队时显示消息。我如何通过处理此自定义消息或任何其他方式的异常来做到这一点?public int dequeue()    {        if(this.isEmpty()){            System.out.println("Queue is Empty !");             return 0;        }        else{             first++;             int x = arr[first];             return x;        }    }
查看完整描述

3 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

public int dequeue() throws ArrayIndexOutOfBoundsException

{

    first++; 

    int x = arr[first]; 

    return x;

}

当你调用它时:


try {

    dequeue();

} catch (ArrayIndexOutOfBoundsException e) {

    System.out.println("Queue is empty!");

}


查看完整回答
反对 回复 2021-09-12
?
绝地无双

TA贡献1946条经验 获得超4个赞

public void dequeue()

    {

        if(this.isEmpty()){

            System.out.println("Queue is Empty !"); 

        }

        else{ 

            first++; 

            int x = arr[first]; 

        }

    }

如果第一个语句的计算结果为 true 而不要求您返回任何内容,则应打印该行


查看完整回答
反对 回复 2021-09-12
?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

在您的情况下,如果您删除 return 语句,程序将正常工作。但是最好使用 return 语句来避免不必要的复杂性。


查看完整回答
反对 回复 2021-09-12
  • 3 回答
  • 0 关注
  • 145 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信