I've been running into the issue of calling the class that's supposed to print even and odd numbers into the main method, printing each number without overwriting the previous one. I've tried replacing the public int with public void, and it still didn't work, if you guys could help me it would help a lot. this is the code:
package myprojects;
import java.util.*;
class ArrayMethod {
private int[] Array;
public void Calc(int[] Array) {
this.Array = Array;
}
public int Sum() {
int sum = 0;
for (int i = 0; i < Array.length; i++) {
sum += Array[i];
}
return sum;
}
public double Average() {
return Sum() / Array.length;
}
public int PrintOdd() {
for (int i = 0; i < Array.length; i++)
if (Array[i] % 2 != 0) {
int Odd = Array[i];
System.out.println(Odd);
}
}
public int PrintEven() {
for (int i = 0; i < Array.length; i++)
if (Array[i] % 2 == 0) {
int Even = Array[i];
System.out.println(Even);
}
}
}
public class ArrayEX {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
ArrayMethod B = new ArrayMethod();
int[] A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
B.Calc(A);
System.out.println("Sum of the numbers inside array is " + B.Sum());
System.out.println("Average of the numbers inside array is " + B.Average());
System.out.println("Even numbers " + B.PrintEven());
System.out.println("Odd Numebrs " + B.PrintOdd());
}
}
int PrintEven()for example needs toreturnsomething but it doesn't. You also try to evaluate (print) the returned result of those methods in"Even numbers " + B.PrintEven()for example. What number(?) should it print in those lines?variableis either and I don't know what you wanted to return from those methods. If they don't need to return a result value (like yourSummethod) , make themvoidand maybe doSystem.out.println("Even numbers"); B.PrintEven();?