+ 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
3 Antworten
+ 5
https://code.sololearn.com/c538CMqP32nd/?ref=app
+ 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
+ 2
thank you so much for helping guys.