[Solved] array of even, odd, and negative integers [closed]


First, I think you want your input loop to look something like this –

for (int x = 0; x < 10; ++x) {
  System.out.println("Enter number: " + (x + 1));
  int num = scan.nextInt(); // <-- get user input to
                            // test.
  // Test for negative first.
  if (num < 0) {
    negativeList[neg++] = num;
  } else {
    // It isn't negative.
    if (num % 2 == 0) {
      // It is even.
      evenList[even++] = num;
    } else {
      // It must be odd.
      oddList[odd++] = num;
    }
  }
}

Second, here is one method for output

// Are there any negative numbers?
if (neg > 0) {
  System.out.println("The negative numbers are: ");
  for (int x = 0; x < neg; x++) {
    if (x != 0) {
      // Add a comma between entries.
      System.out.print(", ");
    }
    // Print the number at x (where x is 0 to neg).
    System.out.print(negativeList[x]);
  }
  // Add a new line.
  System.out.println();
}

Another way to print arrays (if they’re correctly sized) is to use Arrays#toString(int[])

 System.out.println("The negative numbers are: " + Arrays.toString(negativeList));

2

solved array of even, odd, and negative integers [closed]