public InetAddress[] getAllIp()
{
try {
InetAddress adr=InetAddress.getLocalHost();
String name=adr.getHostName();
InetAddress [] ip=InetAddress.getAllByName(name);
return ip;
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
编译通不过,大家帮我找找原因吧,出错原因说是返回值的问题!
{
try {
InetAddress adr=InetAddress.getLocalHost();
String name=adr.getHostName();
InetAddress [] ip=InetAddress.getAllByName(name);
return ip;
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
编译通不过,大家帮我找找原因吧,出错原因说是返回值的问题!
{
InetAddress[] ip = null;
try {
InetAddress adr=InetAddress.getLocalHost();
String name=adr.getHostName();
ip=InetAddress.getAllByName(name); //因为这里是在try内,如果发生异常就没有返回值了,所以编译不能通过
} catch (UnknownHostException e) {
e.printStackTrace();
}
return ip;
}
public InetAddress[] getAllIp() {
InetAddress[] ip=null;
try {
InetAddress adr = InetAddress.getLocalHost();
String name = adr.getHostName();
ip = InetAddress.getAllByName(name);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return ip; }
{
InetAddress[] ip = null;
try {
InetAddress adr=InetAddress.getLocalHost();
String name=adr.getHostName();
ip=InetAddress.getAllByName(name); //因为这里是在try内,如果发生异常就没有返回值了,所以编译不能通过
} catch (UnknownHostException e) {
e.printStackTrace();
}
return ip;
}
这样就可以了.public InetAddress[] getAllIp() {
InetAddress[] ip = null;
try {
InetAddress adr = InetAddress.getLocalHost();
String name = adr.getHostName();
ip = InetAddress.getAllByName(name);
} catch (UnknownHostException e) {
e.printStackTrace();
return null;
}
return ip;
}
{
try {
InetAddress adr=InetAddress.getLocalHost();
String name=adr.getHostName();
InetAddress [] ip=InetAddress.getAllByName(name);
return ip; } catch (UnknownHostException e) {
e.printStackTrace();
InetAddress[] ip=new InetAddress[0];
return ip;
}
}
果然是我想的那样,return放里面了。这错误也犯过。
import java.net.UnknownHostException;public class InetAddressTest { public InetAddress[] getAllIp() {
InetAddress[] ip = null;
try {
InetAddress adr = InetAddress.getLocalHost();
String name = adr.getHostName();
ip = InetAddress.getAllByName(name);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return ip; } public static void main(String[] args) {
// TODO Auto-generated method stub
InetAddressTest inet = new InetAddressTest();
InetAddress[] ip = inet.getAllIp();
for (InetAddress i : ip) {
System.out.println(i); } }}
-------------------------------------------------------------------------------------
如果返回值类型不是void时,需要保证有值返回,例如下面的方法就有语法错误:
public int test(int a){
if(a < 0){
return 0;
}
}
则该方法的声明代码中,当a的值大于等于零时,则没有返回值,这语法上称作返回值丢失,这个也是在书写return语句时需要特别注意的问题。
----------------------------------------------------------------------------------------
你犯的错误就上面的错误