我是 Java 新手。我正在进行一个小程序练习,并遇到缺少返回语句的错误。
有人可以帮忙吗?
import java.util.Scanner;
class nonstatic1
{
public static void main(String[] args)
{
// this method works
nonstatic2 Ref=new nonstatic2();
int Addition=Ref.add();
System.out.println (Addition);
String email=email();
}
// the one below is the one that does not work and gives me the error
public static String email()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter Your email Address");
String email=in.nextLine();
if(email.endsWith(".sc"))
return email;
}
}
If email.endsWith(".sc")
返回 false,该函数没有 return 语句。
由于您将返回类型声明为String
,该函数必须始终返回一个字符串(或 null)。
所以在你的情况下:
if (email.endsWith(".sc")) {
return email;
}
return null; //Will only reach if condition above fails.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)