It's not working as I wasn't to lol | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

It's not working as I wasn't to lol

Ok so, I'm trying to count positive number and negative numbers by using hasnextdouble. the output is not right. after putting all the number just type any letter. https://code.sololearn.com/ct59K446Db58/?ref=app

15th Mar 2018, 8:09 AM
[No Name]
[No Name] - avatar
3 Answers
+ 5
https://code.sololearn.com/c538CMqP32nd/?ref=app
15th Mar 2018, 8:48 AM
Tarantino
Tarantino - avatar
+ 5
Here's your code fixed, now it counts and sum positive & negative numbers. import java.util.Scanner; public class Program { public static void main(String[] args) { double numbers = 0; int positiveNumbers = 0; int negativeNumbers = 0; double totalPositive = 0; double totalNegative = 0; System.out.println("Enter the numbers: "); Scanner input = new Scanner(System.in); while (input.hasNextDouble()) { numbers = input.nextDouble(); System.out.print(numbers + " "); if (numbers >= 0) { totalPositive += numbers; positiveNumbers++; } else { totalNegative += numbers; negativeNumbers++; } } System.out.printf("Total of %d positive numbers is %f\n", positiveNumbers, totalPositive); System.out.printf("Total of %d negative numbers is %f\n", negativeNumbers, totalNegative); } } Hth, cmiiw
15th Mar 2018, 9:41 AM
Ipang
+ 2
thank you so much for helping guys.
15th Mar 2018, 9:49 PM
[No Name]
[No Name] - avatar