为什么我的代码不编译并请求返回语句?



为什么这段代码不能编译?

import javafx.util.Pair;
import java.util.Scanner;
public class Solution {
public static Pair < Integer, Integer > swap(Pair < Integer, Integer > swapValues) {
Scanner scanner =new Scanner(System.in);

int a; //lets say a=2
int b; //and b=3
a= scanner.nextInt();
b= scanner.nextInt();
Pair pr =new Pair 
int swap=a; //swap =a i.e swap is now 2
a=b; //a was 0 now its 3 
b=swap; //b was 0 now its 2
//swap complete now a=3,b=2
}
}

错误:

Compilation Failed
./Solution.java:17: error: missing return statement
}
^
1 error

编译器想从我这里得到什么?

创建方法时,您应该(我认为)始终知道是否希望此方法返回某些内容。在这种情况下,如果我没弄错的话,你没有提到你的返回类型是什么。它是无效的吗?它是一个物体吗?前面提到的解决方案返回您指定为or的Pair类对象。如果这是你想要的回报,那么你可能没问题。谢谢你的宝贵时间。

最新更新