在 Java 中返回一个值
当方法内部有一个 if 语句根据结果返回不同的数据类型时,如何编写一个返回原始数据类型值的方法?
int minimum = 5;
int students = 4;
int studentGrades = 100;
public double getAverage(){
if(students >= minimum){
return studentGrades / students;
}
else {
System.out.println(null, "Sorry, you don't have enough students.");
return false;
}
}
How do I write a method which returns a primitive datatype value when inside the method it has an if statement which returns a different datatype depending on the result?
int minimum = 5;
int students = 4;
int studentGrades = 100;
public double getAverage(){
if(students >= minimum){
return studentGrades / students;
}
else {
System.out.println(null, "Sorry, you don't have enough students.");
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您实际上无法从同一个方法返回两种不同的基本类型。对于引用类型,您可以返回
Object
,但这无论如何都不是一个好主意。您的
return false
看起来像是一个错误条件,因此您可能会考虑在此处抛出异常:You can't really return two different primitive types from the same method. With reference types you could return
Object
, that that isn't a good idea anyway.Your
return false
looks like an error condition, so you might think about throwing an exception here:在您给出的示例中,您应该抛出异常 - 基本上,对象的状态对于方法调用无效。您可能需要使用
IllegalStateException
来实现此目的,或者选择其他类型的异常。 (该异常扩展了 RuntimeException,因此它不是一个已检查的异常。这可能适合也可能不适合您的实际用例。)(顺便说一句,您的示例代码也不会执行您想要的操作 - 如果您希望它执行浮点除法而不是整数除法,则应该将其中一个操作数转换为 double 。)
In the example you've given, you should throw an exception - the state of the object isn't valid for the method call, basically. You might want to use
IllegalStateException
for this, or choose a different type of exception. (That exception extendsRuntimeException
, so it isn't a checked exception. That may or may not appropriate for your real use case.)(As an aside, the division in your sample code won't do what you want either - you should cast one of the operands to
double
if you want it to execute floating point division instead of integer division.)public Object getAverage()
但你应该检查返回类
public Object getAverage()
but then you should check the return class
例如,您可以返回一个包含分数和成功标志的复合对象。
或者在上面的版本中,您可以返回 Double.NaN 来指示失败。或者正如其他人建议的那样,在失败时抛出异常。
You can as example return a composite object that contains the score and a flag for success.
Or in the above version you could return
Double.NaN
to indicate the failure. Or as others have suggested throw an Exception in case of failure.我不是 Java 专家,但我认为这里的良好做法是,如果学生人数少于所需数量,则抛出异常。如果您必须返回两个完全不同的值,我建议您将它们封装在您自己的类中。
I'm no Java expert but I think the good practice here is to throw an exception if you have less than the required amount of students. If you must return 2 completely different values I suggest you encapsulate them in a class of your own.
一个java方法只能有一种返回类型。因此,您应该尝试其他方法来处理这种情况。正如上面其他人指出的那样,您可以抛出异常。或者,您可以使用可为 null 的数据类型,并在此特殊条件为 true 时返回 null。但这很丑陋,不推荐。
A java method can have only one return type. So you should try some other approche in handling this situation. As others above have pointed out you could throw a exception. Or you could use a nullable data type, and return null when this special condition is true. But this is ugly and is not recommended.