简体   繁体   English

如何从int方法打印字符串而不返回int?

[英]How to print string from an int method without returning int?

I am working on a method.我正在研究一种方法。 It returns int .它返回int

int top() {
   return q.peek();
}

The method above works fine for non-empty queue.上述方法适用于非空队列。 It is not for empty queue.它不适用于空队列。

When I modify this code to handle empty queue, it will be like this当我修改这段代码来处理空队列时,它会是这样的

int top() {
   if (q.isEmpty()) {
      System.out.println("queue is empty"); // I want to end the process here 
      return -1; // I don't want this line to return -1
   }
   return q.peek(); // I don't want this line to execute
}

System.out.println(q.top());

The result will be like this if the queue is empty如果队列为空,结果会是这样

queue is empty
-1

I want it to print only queue is empty , not -1.我希望它只打印queue is empty ,而不是 -1。

So is there a way to modify only top() method for this case in Java?那么有没有办法在 Java 中只修改这种情况下的top()方法?

If you are able to change System.out.println(q.top());如果你能够改变System.out.println(q.top()); to a call to only q.top();只调用q.top(); you can simply just print from inside the method:您可以简单地从方法内部打印:

int top() {
   if (q.isEmpty()) {
      System.out.println("queue is empty");
      return -1;
   }
   System.out.println(q.peek());
   return q.peek();
}

If you are unable to change ANYTHING about the System.out.println(q.top()) statement you would need to return a String instead and use Integer.toString to convert the value to a String :如果您无法更改System.out.println(q.top())语句的任何内容,则需要返回String并使用Integer.toString将值转换为String

String top() {
    if (q.isEmpty()) {
        return "queue is empty";
    }
    return Integer.toString(q.peek());
}

Are you wanting to print the int returned only if the queue is not empty, otherwise print "queue is empty"?您是否只想在队列不为空时打印返回的 int,否则打印“队列为空”? If so, how about doing something like this:如果是这样,如何做这样的事情:

int top() {
  if (q.isEmpty()) {
    return -1;
  }
  return q.peek();
}

System.out.println(q.top() == -1 ? "queue is empty" : q.top());

And another option:还有另一个选择:

int top() throws Exception {
  if (q.isEmpty()) {
    throw new Exception("queue is empty");
  }
  return q.peek();
}

try {
  System.out.println(q.top());
} catch (Exception e) {
  System.out.println(e.getMessage());
}

When a method signature return int , you should not try to return another type as it can confuse other members who read your code.当方法签名返回int ,您不应该尝试返回另一种类型,因为它会混淆阅读您代码的其他成员。

If you cannot modify the System.out.println() method, the best option is to throw an Exception in your method like the answer given by Caitlyn Wiley.如果您无法修改System.out.println()方法,最好的选择是在您的方法中抛出一个异常,就像 Caitlyn Wiley 给出的答案一样。

Another acceptable way is to return an int that signify error (like you did with -1), as long as you have good docs to explain it.另一种可接受的方法是返回一个表示错误的 int(就像你对 -1 所做的那样),只要你有很好的文档来解释它。

I've just found another way to solve this problem without changing top() data type.我刚刚找到了另一种无需更改top()数据类型即可解决此问题的方法。

int top() {
    if (q.isEmpty())
        System.out.println("queue is empty");
        System.exit(0);
    return q.peek();
}

What do you guys think?你们有什么感想?

I thank you every answer to my question.我感谢你对我问题的每一个回答。 I am very grateful.我很感激。 All answers are very useful.所有的答案都非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM