java错误中缺少return语句} [英] Missing return statement } in java error

查看:95
本文介绍了java错误中缺少return语句}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白为什么我的代码会导致这个错误,一切看起来都是正确的,我以为它不断出现,因为它缺少返回语句}

I really can't figure out why my code is causing this error, everything looks correct, thought it keeps coming up as it is missing a return statement }

我尝试寻找解决方案,我看到if"之后的while"是一种解决方案,但由于我需要多个数字,因此不能使用 while,必须使用what if"

I tried looking for solutions, and I saw that "while" after "if" is one solution, but since I need multiple numbers I cannot use while, and have to go with "what if"

谁能帮帮我?

import java.util.*;

class WS8Q4 
{
    public static void main (String[] args)
    {
        Scanner in = new Scanner(System.in);

        int x = 0;

        System.out.println("Please put in an integer from 0 - 9");
        x = in.nextInt ();

        String answer = numTxt (x);
        System.out.println(answer);
}

public static String numTxt (int x)
    {
        if (x==0)
        {
            return ("Zero");
        }
        else if (x==1)
        {
            return ("One");
        }
        else if (x==2)
        {
            return ("Two");
        }
        else if (x==3)
        {
            return ("Three");
        }
        else if (x==4)
        {
            return ("Four");
        }
        else if (x==5)
        {
            return ("Five");
        }
        else if (x==6)
        {
            return ("Six");
        }
        else if (x==7)
        {
            return ("Seven");
        }
        else if (x==8)
        {
            return ("Eight");
        }
        else if (x==9)
        {
            return ("Nine");
        }
    }
} 

推荐答案

如果 x 不是 0-9 怎么办?您没有针对该案例的 return 语句.将它添加到底部,在最后一个 else if 下方:

What if x is something besides 0-9? You don't have a return statement for that case. Add it at the bottom, below your last else if:

return "Other";

这篇关于java错误中缺少return语句}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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