我是Java新手。我正在做一个小型程序实践,并且会丢失return语句错误。
有人可以帮忙吗?
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语句上。您缺少else分支。当表达式的计算值为时false,您的程序将不返回任何内容,因此将返回missing return statement错误。
IF
else
false
missing return statement
将其更改为如下所示:
if(email.endsWith(".sc")) return email; else return "invalid email";