Return Helper 方法中的语句 (Java)
Return statement in Helper Method(Java)
我正在练习 codingbat 中的简单编码问题。其中一个问题是要求我使用 helper method
来防止冗余代码。但是,我很迷茫,因为我不知道为什么我应该使用 public
和 int
作为这个问题的 return 类型。(因为问题要求我使用 header下)
public int fixTeen(int n)
辅助方法中的 return 有什么作用?另外,我怎么知道我应该为我的辅助方法使用 private 还是 public?
请看我的代码。
// Given 3 int values, a b c, return their sum. However, if any of the values
// is a teen -- in the range 13..19 inclusive -- then that value counts as 0,
// except 15 and 16 do not count as a teens. Write a separate helper
// "public int fixTeen(int n) {"that takes in an int value and returns that value
// fixed for the teen rule. In this way, you avoid repeating the teen code 3
// times (i.e. "decomposition"). Define the helper below and at the same
// indent level as the main noTeenSum().
public int noTeenSum(int a, int b, int c) {
return fixTeen(a) + fixTeen(b) + fixTeen(c);
}
public int fixTeen(int n) {
if (n >= 13 && n <= 19 && n != 15 && n != 16)
n = 0;
return n;
}
编辑:
为辅助方法设置 return 类型 void
和 int
有什么区别?起初,我认为 return int
是不必要的,并尝试将 return 类型设置为 void
但它给了我一个错误。
一般来说,至少在 java 的开头,方法应该命名为 public。稍后,当您开始面向对象编程时,它所在的区域(public 或私有)更重要。例如,添加关键字“public”意味着可以在 class 之外访问该值,而“private”则意味着不能。当您不希望最终用户能够访问您的私人数据时,这很重要。
要点是,当你制作一个方法时,现在将它们设置为 public。
接下来是辅助方法。在“public”或“private”之后,您有 return 类型。您已将其设置为“int”。因此,return 类型必须是整数。它不能是字符串或双精度数 - 它必须是整数。如果您将 return 值设置为“void”,那么就不会有 return 值,如果您尝试编写“return(n);”,它会给您一个错误。
所以 TLDR:它被命名为“public”,因为你希望能够在 class 之外访问这个方法,它说“int”,因为你需要 return 整数类型。然后,当你 return(n) 时,它会给出值,比如 a == 7,如果 b == 18,它会设置 b == 0。之后,它会添加数字在一起,你就有了答案!
我正在练习 codingbat 中的简单编码问题。其中一个问题是要求我使用 helper method
来防止冗余代码。但是,我很迷茫,因为我不知道为什么我应该使用 public
和 int
作为这个问题的 return 类型。(因为问题要求我使用 header下)
public int fixTeen(int n)
辅助方法中的 return 有什么作用?另外,我怎么知道我应该为我的辅助方法使用 private 还是 public?
请看我的代码。
// Given 3 int values, a b c, return their sum. However, if any of the values
// is a teen -- in the range 13..19 inclusive -- then that value counts as 0,
// except 15 and 16 do not count as a teens. Write a separate helper
// "public int fixTeen(int n) {"that takes in an int value and returns that value
// fixed for the teen rule. In this way, you avoid repeating the teen code 3
// times (i.e. "decomposition"). Define the helper below and at the same
// indent level as the main noTeenSum().
public int noTeenSum(int a, int b, int c) {
return fixTeen(a) + fixTeen(b) + fixTeen(c);
}
public int fixTeen(int n) {
if (n >= 13 && n <= 19 && n != 15 && n != 16)
n = 0;
return n;
}
编辑:
为辅助方法设置 return 类型 void
和 int
有什么区别?起初,我认为 return int
是不必要的,并尝试将 return 类型设置为 void
但它给了我一个错误。
一般来说,至少在 java 的开头,方法应该命名为 public。稍后,当您开始面向对象编程时,它所在的区域(public 或私有)更重要。例如,添加关键字“public”意味着可以在 class 之外访问该值,而“private”则意味着不能。当您不希望最终用户能够访问您的私人数据时,这很重要。
要点是,当你制作一个方法时,现在将它们设置为 public。
接下来是辅助方法。在“public”或“private”之后,您有 return 类型。您已将其设置为“int”。因此,return 类型必须是整数。它不能是字符串或双精度数 - 它必须是整数。如果您将 return 值设置为“void”,那么就不会有 return 值,如果您尝试编写“return(n);”,它会给您一个错误。
所以 TLDR:它被命名为“public”,因为你希望能够在 class 之外访问这个方法,它说“int”,因为你需要 return 整数类型。然后,当你 return(n) 时,它会给出值,比如 a == 7,如果 b == 18,它会设置 b == 0。之后,它会添加数字在一起,你就有了答案!