java - 为什么我不能在 Java 代码中使用“return”?
问题描述
我在第四行的 Java 代码有问题。我有这个错误:“此方法必须返回 int 类型的结果”。所以我没有返回'c'。我怎样才能回来?
public class bese_bolunme {
static int function(int b)
{
for (int c=0;c<b;c++)
{
if(c%5==0)
{
System.out.println(c);
return c;
}
}
}
public static void main(String[] args) {
function(36);
}
解决方案
由于您在函数签名中声明它返回一个整数,因此您必须在函数的所有执行流程中返回一个整数。
static int function(int b)
{
for (int c=0;c<b;c++)
{
if(c%5==0)
{
System.out.println(c);
return c;
}
} //end for loop
return -1; //Or other logic you prefer
}
推荐阅读
- firebase - GCM 和 FCM 设备令牌在格式上的区别
- css - 如何添加浮动图像以使其在屏幕上保持可见而不管滚动?
- ios - iOS objC将文件上传到firebase没有发生
- react-native-android - 本机基础收音机颜色不起作用?
- python - 在 Python 中创建的 bash 文件中的引号
- scala - 前缀跨度输出格式
- oracle - sql developer和存储过程之间的输出不一致
- python - Charmap 编解码器字符编码错误
- sql - 修改 SQL 中的 JSON 字符串(日期时间条目)
- rust - 在没有 std 的情况下将 *mut u8 转换为 &[u8]