在辅助方法中的返回语句(Java)

huangapple go评论76阅读模式
英文:

Return statement in Helper Method(Java)

问题

我正在练习来自CodingBat的简单编程问题。其中一个问题要求我使用“helper method”来防止冗余代码。然而,我感到非常迷惑,因为我不知道为什么在此问题中我应该使用publicint作为返回类型。(因为问题要求我在下面使用头部)

public int fixTeen(int n)

这个辅助方法的返回值有什么作用?另外,我怎么知道我应该在我的辅助方法中使用private还是public

请看看我的代码。

// 给定 3 个 int 值,a b c,返回它们的和。然而,如果其中任何一个值是青少年 - 在包括 13 到 19 在内的范围内 - 那么该值视为 0,除了 15 和 16 不视为青少年。编写一个单独的辅助方法
// "public int fixTeen(int n) {",该方法接受一个 int 值,并返回适用青少年规则的修正值。通过这种方式,您避免了重复编写三次青少年代码(即“分解”)。将该辅助方法定义在与主方法 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;
}

编辑:
在辅助方法中设置返回类型为 voidint 有什么区别?起初,我认为 return int 是不必要的,尝试将返回类型设置为 void,但是它给了我一个错误。

英文:

I am practicing simple coding problems from codingbat. One of the problems are asking me to use helper method to prevent redundant codes. However, I am very lost because I do not know why I should use public and int as return type for this problem.(because the question asks me to use header below)

> public int fixTeen(int n)

What does the return from the helper method doing? Also, how do I know if I should use private or public for my helper method?

Please take a look at my code.

// 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 
// &quot;public int fixTeen(int n) {&quot;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. &quot;decomposition&quot;). 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 &gt;= 13 &amp;&amp; n &lt;= 19 &amp;&amp; n != 15 &amp;&amp; n != 16)
    n = 0;
  return n;
}

Edit:
What is the difference between setting return type void and int for the helper method? At first, I thought return int is unnecessary and tried to set the return type as void but it gave me an error.

答案1

得分: 3

一般来说,至少在 Java 的初始阶段,方法的命名应为 public。稍后,当你进入面向对象编程时,方法所在的访问控制区域(public 或 private)变得更加重要。例如,添加关键字 "public" 表示该值可以在类的外部访问,而 "private" 表示不能访问。当你不希望最终用户能够访问你的私有数据时,这一点非常重要。

关键是,在创建方法时,暂时将它们设置为 public。

接下来是辅助方法。在 "public" 或 "private" 后面,你有一个返回类型。你将其设置为 "int"。因此,返回类型必须是整数。它不能是字符串或双精度浮点数 - 它必须是整数。如果你将返回值设置为 "void",那么就不会有返回值,如果尝试编写 "return(n);",会导致错误。

因此,简而言之:它被命名为 "public",因为你希望能够在类的外部访问此方法,并且它指定为 "int",因为你需要返回整数类型。然后,当你返回(n)时,它将赋予该值,比如 a == 7,如果 b == 18,它将把 b == 0。之后,它将这两个数字相加,于是你就得到了答案!

英文:

In general, at least for the beginnings of java, methods should be named public. Later on, when you get to object oriented programming, the area it's in (public or private) matters more. For example, adding the keyword "public" means that that value can be accessed outside of the class, while "private" means it cannot. This is important for when you don't want the end user to be able to access your private data.

Point is, when you make a method, for now have them set to public.

Next up is the helper method. After the "public" or "private", you have the return type. You have it set to "int". Therefore, the return type must be an integer. It can't be a string, or a double - it must be an integer. If you set the return value to "void", then there would be no return value, and if you tried to write "return(n);", it would give you an error.

So TLDR: It's named "public" because you want to be able to access this method outside of the class, and it says "int", because you need to return an integer type. Then, when you return(n), it'll give the value, say, a == 7, and if b == 18, it'll set b == 0. After that, it adds the numbers together, and you have your answer!

huangapple
  • 本文由 发表于 2020年10月27日 09:50:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64547285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定