java 创建一个可以计算数组中负数的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12775133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create a method that can count negative numbers in an array
提问by PHZE OXIDE
public class negativeTest {
public static int Negativenum (int[] array) {
int negative = 0;
for (int i = 0; i < array.length; i++){
if(array[i] < 0){
negative = negative + 1;
}
System.out.println(negative);
}
}
}
I am trying to count how many elements in array are negative. This is what i have so far. My question is: eclipse is telling me that i should return a void instead of static int? How can i do this without using void?
我试图计算数组中有多少元素是负数。这是我到目前为止。我的问题是:eclipse 告诉我我应该返回一个 void 而不是 static int?我如何在不使用 void 的情况下做到这一点?
I'd want to use
我想用
public static int negativenum(int[] array){
Only way i can get this working is create an array with positive and negative numbers and count them, but i want to be able to have method that does that without creating array of numbers. Can you help me?
我可以让这个工作的唯一方法是创建一个包含正数和负数的数组并计算它们,但我希望能够拥有不创建数字数组的方法。你能帮助我吗?
回答by vikiiii
Try giving a return statement , your method is expecting a int as a return parameter. Therefore it will give compiler error.
尝试给出一个 return 语句,您的方法需要一个 int 作为返回参数。因此它会给出编译器错误。
public class negativeTest {
public static int Negativenum (int[] array) {
int negative = 0;
for (int i = 0; i < array.length; i++){
if(array[i] < 0){
negative = negative + 1;
}
System.out.println(negative);
}
return negative;
}
}
Ther error you are getting is because you have not declared the main
function inside the class.
你得到的错误是因为你没有main
在类中声明函数。
You have to call Negativenum
from the main function.
您必须Negativenum
从主函数调用。
you can do it like this :
你可以这样做:
public static void main (String args[])
{
negativeTest nt = new negativeTest();
int [] array = new int[]{ 100,200 };
int count = nt.Negativenum(array);
System.out.println(count); // It will print **2**
}
Regarding your doubts you have asked in comments.
关于您在评论中提出的疑问。
You have to return anything from function only when you want to use that use that return value from the calling function. Otherwise if you want to just print that value on console or log that value , you can easily do it in the negativeTest function and you can change the return type of this function to void.
仅当您想使用调用函数的返回值时,才必须从函数返回任何内容。否则,如果您只想在控制台上打印该值或记录该值,您可以在negativeTest 函数中轻松完成,并且可以将此函数的返回类型更改为void。
FYI , you should not begin your classname with the lower case character.
仅供参考,您不应该以小写字符开头您的类名。
回答by Burhan Khalid
Your function signature suggest a return type of int, but you aren't returning anything from the function. I suspect this is why Eclipse is suggesting you change the function signature to return void.
您的函数签名建议返回类型为 int,但您没有从函数返回任何内容。我怀疑这就是 Eclipse 建议您更改函数签名以返回 void 的原因。
If you add return negative;
it should avoid the notice from Eclipse.
如果添加return negative;
它应该避免来自 Eclipse 的通知。
If your intention is to simply print the count, then you should change the return type.
如果您的目的是简单地打印计数,那么您应该更改返回类型。
回答by codaddict
The error is because you are not returning anything from the function which is expected to return an int
.
错误是因为您没有从预期返回int
.
If you want the function to count the number of negative numbers and return the count so that the caller of the function gets the count, you can add an
如果你想让函数计算负数的个数并返回计数以便函数的调用者得到计数,你可以添加一个
return negative;
before the end of the function.
在函数结束之前。
Alternatively if you don't want to return anything from the function and want to just print the count as part of the function call, you can change the return type of the function from int
to void
:
或者,如果您不想从函数返回任何内容,而只想将计数作为函数调用的一部分打印出来,您可以将函数的返回类型从int
更改为void
:
public static void Negativenum (int[] array) {
回答by Wanabrutbeer
if you dont want to return anything, set your method signature to void, but add an out variable, like so:
如果不想返回任何内容,请将方法签名设置为 void,但添加一个 out 变量,如下所示:
public static void NegativeNum(int[] array, out int negative)
{
negative = 0;
foreach(int i in array) { if (i < 0) negative++;
}
then you just declare negative wherever this method is called from, and pass it in as an out variable:
然后您只需在调用此方法的任何地方声明否定,并将其作为输出变量传入:
int negative = 0;
NegativeNum(array, out negative);
After that call, negative will contain the count of negative numbers determined by the method.
在该调用之后,negative 将包含由该方法确定的负数的计数。