在这个函数中,如果 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!");
}
绝地无双
TA贡献1946条经验 获得超4个赞
public void dequeue()
{
if(this.isEmpty()){
System.out.println("Queue is Empty !");
}
else{
first++;
int x = arr[first];
}
}
如果第一个语句的计算结果为 true 而不要求您返回任何内容,则应打印该行
添加回答
举报
0/150
提交
取消