【发布时间】:2014-01-09 15:50:49
【问题描述】:
我真的不知道为什么我的代码会导致这个错误,一切看起来都是正确的,认为它不断出现,因为它缺少返回语句}
我尝试寻找解决方案,我发现“if”之后的“while”是一种解决方案,但由于我需要多个数字,我不能使用 while,而必须使用“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");
}
}
}
【问题讨论】:
-
尝试使用
switch块。 -
你必须返回一个值。如果
x == 10,你会返回什么?