不理解遗漏的退货声明 [英] Not understanding missing return statement

查看:128
本文介绍了不理解遗漏的退货声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java新手。我正在进行一个小程序练习,并且错过了返回语句错误。

I am new to Java. I am working on a small program practice and getting missing return statement error.

有人可以帮忙吗?

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 时,您的程序不会返回任何内容,因此缺少return语句错误。

The problem is with the IF statement. You are missing the else branch. When the evaluated value of the expression is false, your program doesn't return anything, hence the missing return statement error.

将其更改为:

if(email.endsWith(".sc"))
          return email;
else
          return "invalid email";

这篇关于不理解遗漏的退货声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆