The program should return how many positive and negative numbers there are in a sequence.
The main method will read a sequence of numbers from input and for each of them evaluate if it is positive or negative using this method below.
public static int positiveOrNegativeNumber(int n) {
int positiveNumber; //counter for positive numbers
int negativeNumber; //counter for negative numbers
positiveNumber=0;
negativeNumber=0;
if (n>0) {
positiveNumber = positiveNumber + 1;
} else if (n<0) {
negativeNumber = negativeNumber + 1;
}
return positiveNumber & negativeNumber;
}
does the expression return positiveNumber & negativeNumber return them both?
edit: So, following Java Geo suggestion I got this:
public static NumCounter positiveOrNegativeNumber(int n, NumCounter numCounter) {
if (n>0) {
numCounter.setPositiveNumCounter(numCounter.getPositiveNumCounter()+1);
} else if (n<0) {
numCounter.setNegativeNumCounter(numCounter.getNegativeNumCounter()+1);
}
return numCounter;
}
But I don't know what I should add in the main method to print it out
public static void main(String[] args) {
int n;
while(!Lettore.in.eoln()) { //while the line has not ended
n = Lettore.in.leggiInt(); //read n from input
// what is missing here??
}
}
Don't worry about Lettore.in.eoln and Lettore.in.leggiInt, are just methods that make getting things from input easier without having to use the scanner.