Java 为什么这个公共字符串函数不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18664211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why isn't this public String function working?
提问by Werg Asdfwer
It is saying that it must return a string but I don't see anything wrong with it? I think numericDayOfWeek should be working fine?
它说它必须返回一个字符串,但我看不出有什么问题?我认为 numericDayOfWeek 应该可以正常工作吗?
public String getDayOfWeek(){
if(numericDayOfWeek==0){
return "Saturday";
}
if(numericDayOfWeek==1){
return "Sunday";
}
if(numericDayOfWeek==2){
return "Monday";
}
if(numericDayOfWeek==3){
return "Tuesday";
}
if(numericDayOfWeek==4){
return "Wednesday";
}
if(numericDayOfWeek==5){
return "Thursday";
}
if(numericDayOfWeek==6){
return "Friday";
}
}
Here is the full code
这是完整的代码
public class DayOfWeek {
int myMonth, myDayOfMonth, myYear, myAdjustment, numericDayOfWeek;
public DayOfWeek(int month, int dayOfMonth, int year){
myMonth = month;
myDayOfMonth = dayOfMonth;
myYear = year;
}
public int getNumericDayOfWeek(){
if(myMonth==1){
myAdjustment = 1;
if(myYear%4==0){
myAdjustment-=1;
}
}
if(myMonth==2){
myAdjustment = 4;
if(myYear%4==0){
myAdjustment-=1;
}
}
if(myMonth==3){
myAdjustment = 4;
}
if(myMonth==4){
myAdjustment = 0;
}
if(myMonth==5){
myAdjustment = 2;
}
if(myMonth==6){
myAdjustment = 5;
}
if(myMonth==7){
myAdjustment = 0;
}
if(myMonth==8){
myAdjustment = 3;
}
if(myMonth==9){
myAdjustment = 6;
}
if(myMonth==10){
myAdjustment = 1;
}
if(myMonth==11){
myAdjustment = 4;
}
if(myMonth==12){
myAdjustment = 6;
}
int fourDivides = myYear / 4;
numericDayOfWeek = myAdjustment + myDayOfMonth + (myYear-1900) + fourDivides;
return numericDayOfWeek;
}
public String getDayOfWeek(){
if(numericDayOfWeek==0){
return "Saturday";
}
if(numericDayOfWeek==1){
return "Sunday";
}
if(numericDayOfWeek==2){
return "Monday";
}
if(numericDayOfWeek==3){
return "Tuesday";
}
if(numericDayOfWeek==4){
return "Wednesday";
}
if(numericDayOfWeek==5){
return "Thursday";
}
if(numericDayOfWeek==6){
return "Friday";
}
}
public int getMonth(){
}
public String getMonthString(){
}
public int getDayOfMonth(){
}
public int getYear(){
}
}
回答by Sotirios Delimanolis
If none of the conditions passes, ie. they all evaluate to false
, the method wouldn't return anything. Add a default return at the end
如果没有一个条件通过,即。他们都评估为false
,该方法不会返回任何内容。最后添加默认返回
public String getDayOfWeek(){
if(numericDayOfWeek==0){
return "Saturday";
}
if(numericDayOfWeek==1){
return "Sunday";
}
if(numericDayOfWeek==2){
return "Monday";
}
if(numericDayOfWeek==3){
return "Tuesday";
}
if(numericDayOfWeek==4){
return "Wednesday";
}
if(numericDayOfWeek==5){
return "Thursday";
}
if(numericDayOfWeek==6){
return "Friday";
}
return "Error";
}
The compiler considers all paths. If none if the if
statements was executed it wouldn't have anything to return
. In that case, it won't be able to compile because the method wouldn't guarantee the contract specified by its definition, ie. to return a String
.
编译器会考虑所有路径。如果没有,如果if
语句被执行,它就没有任何东西return
。在这种情况下,它将无法编译,因为该方法不能保证其定义指定的合同,即。返回一个String
.
Follow the comments or the other answer on how to possibly makes this perform better or make it easier to read (switch-case
).
按照评论或其他答案来了解如何可能使此性能更好或使其更易于阅读 ( switch-case
)。
回答by Jordan Kaye
Sotirios is correct, but a better solution here would be to use a case statement:
Sotirios 是正确的,但这里更好的解决方案是使用 case 语句:
switch(numericDayOfWeek)
{
case 0:
return "Saturday";
case 1:
return "Sunday";
case 2:
return "Monday";
case 3:
return "Tuesday";
case 4:
return "Wednesday";
case 5:
return "Thursday";
case 6:
return "Friday";
default:
return "Error";
}
回答by Omar Mainegra
This should work:
这应该有效:
public String getDayOfWeek(){
if(numericDayOfWeek==0){
return "Saturday";
}
else if(numericDayOfWeek==1){
return "Sunday";
}
else if(numericDayOfWeek==2){
return "Monday";
}
else if(numericDayOfWeek==3){
return "Tuesday";
}
else if(numericDayOfWeek==4){
return "Wednesday";
}
else if(numericDayOfWeek==5){
return "Thursday";
}
else if(numericDayOfWeek==6){
return "Friday";
}
else{
return "Error";
}
}
回答by Trevor Freeman
The reason for the compiler error is that the compiler cannot be certain that your code will always return a String from your method.
编译器错误的原因是编译器不能确定你的代码总是从你的方法返回一个字符串。
In the event that numericDayOfWeek is not in the range of 0 to 6, your function does not specify what value should be returned, and there is no way for the compiler to know or guarantee that numericDayOfWeek will always be within the desired range.
如果 numericDayOfWeek 不在 0 到 6 的范围内,您的函数不会指定应该返回什么值,并且编译器无法知道或保证 numericDayOfWeek 将始终在所需的范围内。
Unfortunately, the compiler is limited in its ability to ensure a return
statement even in simple cases. Take the trivial (and useless) method below:
不幸的是,return
即使在简单的情况下,编译器在确保语句的能力方面也是有限的。采取下面的琐碎(和无用)方法:
// I have a compiler error!
public boolean testReturn()
{
final boolean condition = true;
if (condition) return true;
if (!condition) return false;
}
The above will result in a compiler error saying the method must return a type of boolean. We could easily fix it by changing the second if
statement to be an else
clause since that is one of the few ways that allow the compiler to ensure one or the other blocks of code are guaranteed to be executed.
以上将导致编译器错误,说明该方法必须返回布尔类型。我们可以通过将第二个if
语句更改为else
子句来轻松修复它,因为这是允许编译器确保保证执行一个或其他代码块的少数方法之一。
// I compile!
public boolean testReturn()
{
final boolean condition = true;
if (condition) return true
else return false;
}
The rules are that a method with a return type must not complete normallyand must instead complete abruptly(abruptly here indicating via a return
statement or an exception) per JLS 8.4.7. The compiler looks to see whether normal termination is possible based on the rules defined in JLS 14.21 Unreachable Statementsas it also defines the rules for normal completion.
该规则是用返回类型的方法不能完成正常而必须完成突然(突然在这里通过表示return
每个语句或异常)JLS 8.4.7。编译器会根据JLS 14.21 Unreachable Statements中定义的规则查看是否可以正常终止,因为它还定义了正常完成的规则。
In the case of your specific example, I would suggest considering throwing an IllegalArgumentException
as the last line of your method and replacing your if
statement with a switch
statement. E.g.
在您的具体示例的情况下,我建议考虑将 anIllegalArgumentException
作为方法的最后一行并用if
语句替换您的switch
语句。例如
public String getDayOfWeek()
{
switch(numericDayOfWeek)
{
case 0: return "Saturday";
case 1: return "Sunday";
case 2: return "Monday";
case 3: return "Tuesday";
case 4: return "Wednesday";
case 5: return "Thursday";
case 6: return "Friday";
}
throw new IllegalArgumentException("numericDayOfWeek is out of range: " + numericDayOfWeek);
}
You can also throw the exception in a default
clause of the switch
statement, but in this case I would say that is just a matter of personal preference and I prefer outside of the switch
here.
您也可以default
在switch
语句的子句中抛出异常,但在这种情况下,我会说这只是个人喜好问题,我更喜欢switch
这里之外的内容。