【发布时间】:2011-10-12 18:18:54
【问题描述】:
我正在尝试测试我在数组中查找奇数个数的方法是否适用于 System.out.println() 调用。我知道数组本身没有问题,因为我已经使用 toString() 调用成功打印了它。这是我的方法:
public static int ODD(int[] oddnumbers)
{
int countOdds = 0;
for(int i = 0; i < oddnumbers.length; i++)
{
if(oddnumbers[i] % 2 == 1) // check if it's odd
countOdds++; // keep counting
}
return countOdds;
}
然后在之前的 main 方法中,我调用了 ODD 并使用 System.out.println 对其进行了测试:
public static void main(String args[])
{
ODD(randomThirty); // will find how may numbers in the given numbers (from the array) are ODD numbers and return this count to main method.
System.out.println("And here are how many odd numbers there are in that array: " + countOdds);
}
基本上我的问题是,如何将return countOdds 放入一个变量中,我可以通过main 方法将其传递到System.out.println() 中打印?
【问题讨论】: