编写使用剩余运算器(%)确定整数是否为偶数的方法isEven。该方法应采用整数数组参数,确定数组元素的值是否为偶数,然后打印该值。使用for循环迭代数组。将此方法集成到传递数组的应用程序inn NUMS[] { 8,16,9,52,3,15,27,6,14,25,2,10}中
我试着解决这个问题已经有一段时间了,我坚持到目前为止
public class IsEven
{
public static void main( String[] args )
{
int[] nums = {8, 16, 9, 52, 3, 15, 27, 6, 14, 25, 2, 10 };
System.out.printf( "%s%11s\n", "Number", "Even" ); // column heading
for ( int counter = 0; counter < nums.length; counter++ )
{
if ( IsEven( nums ) )
System.out.printf( "Even numbers are = %d\n", nums );
}
}
public boolean isEven( int even )
{
return even % 2 == 0;
}
} 我能得到帮助吗!
发布于 2013-12-17 14:30:50
你可以像这样改变它。
一定要比较不同之处
在此代码与您的代码之间
看看你的错误在哪里。
public class IsEven {
public static void main(String[] args) {
// initializer list specifies the value for each element
int[] nums = { 8, 16, 9, 52, 3, 15, 27, 6, 14, 25, 2, 10 };
System.out.printf("%11s%11s\n", "Number", "Even"); // column heading
// output each array element's value
for (int counter = 0; counter < nums.length; counter++) {
if (isEven(nums[counter])) {
// System.out.printf("Even numbers are = %d\n", nums[counter]);
System.out.printf("%11d%11s\n", nums[counter], "Yes"); // column heading
}else{
System.out.printf("%11d%11s\n", nums[counter], "No"); // column heading
}
}
} // end method main
// return true if Array is Even
public static boolean isEven(int even) {
return even % 2 == 0;
} // end method boolean
} // end class IsEven发布于 2013-12-17 14:27:14
换行
if ( IsEven( nums ) )至
if ( IsEven( nums[counter] ) )发布于 2013-12-17 14:29:56
您有三个编译时问题:
isEven调用非静态方法main。IsEven而不是isEvenarray of int而不是int传递给isEven方法还有一个运行时问题:
格式化输出"Even numbers are = %d\n", nums (应该是"Even numbers are = %d\n", nums[counter] )
https://stackoverflow.com/questions/20636627
复制相似问题