0
What's wrong with my code??( Beginner)
Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered. https://code.sololearn.com/cz6GmvyiONpe/?ref=app
2 Answers
0
Hello,
my humble guess, your program should be inside a while loop with ending condition.
Something like that:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number or 1001001 to quit!");
int input = 0;
int number;
int zeros =0;
int positives =0;
int negatives = 0;
while(input !=1001001) {
input = scanner.nextInt();
if (input == 0) {
zeros++;
}
else if (input > 0){
positives++;
}
else {
negatives++;
}
}
System.out.println("you entered " + zeros + " zero(s), " + positives + " positive number(s) and " + negatives + " negative number(s) :)");
}
0
it is recursive solution and works, but if you run online use one Scanner
public class Program {
static Scanner sc =
new Scanner(System.in);
input:
1 1 1 2 1 -3 1 -4 1 -5 1 0 0