import java.util.Scanner;
import java.util.Arrays;
/**
This class prints the numeric value of a letter grade given by the user.
*/
public class Numbers
{
int countEven=0;
int countOdd=0;
private int[] digits;
private int[] evenoddCount;
Scanner input = new Scanner(System.in);
/**
Constructs numbers set to 0
@param anEfficiency the fuel efficiency of the car
*/
public Numbers()
{
digits = new int[10];
}
/**
collects 10 numbers from user and places then into array
@return the gradeValue
*/
public void inputArray(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
{
digits = {a, b, c, d, e, f, g, h, i, j};
}
/**
counts even and odds
@return numeric grade
*/
public void evenOdds()
{
for(int i=0; i < digits.length; i++)
{
if(digits[i]%2 == 0)
countEven++;
else
countOdd++;
evenoddCount = {countEven, countOdd};
}
}
/**
prints out the array of 10 positive integers
@return numeric grade
*/
public void printArray()
{
System.out.println(Arrays.toString(evenoddCount));
}
}
}
代码应该使用数组找到奇数。由于某种原因,评论被搞砸了。我似乎在第31行附近搞砸了,由于某种原因,这几乎搞砸了程序的其余部分?我想我搞砸了我的宣言。
答案 0 :(得分:1)
我认为问题出在你分配给数组时。你这样做:
digits = {a, b, c, d, e, f, g, h, i, j };
当你应该这样做时:
digits = new int[] {a, b, c, d, e, f, g, h, i, j};
此外,不是每次都为evenOddCount分配一个新数组,而只是在循环中递增,如下所示:
for(int i=0; i < digits.length; i++)
{
if(digits[i]%2 == 0)
evenoddCount[0]++;
else
evenoddCount[1]++;
}
当你声明你的evenOddCount数组时,声明它是这样的:
int[] evenOddCount = {0,0};