I'm trying to do four operations.
Find the minimum including 0 # (working on)
find the sum of the negative # (Think I'm done)
Find the sum of the odd # (working on)
How many positive #(working on)
I need help on the sum of negative
This is what will be inputed 4 2 0 1 3
this is what i get

Program Output
The minimum integer is 2
The sum of the negative integers is 0
The sum of the odd integers is 0
The number of positive integers in the sequence is 2

Expected Output
The minimum integer is 0
The sum of the negative integers is 0
The sum of the odd integers is 0
The number of positive integers in the sequence is 2

import java.util.Scanner;  // use the Scanner class located in the "java.util" directory




public class Assignment2
{
  public static void main (String[] args)
  {

        int num, min,max, odd=0 ,sum=0,count = 0;
        int MAX = 2147483647;//Maximun number one can input
        Scanner scan = new Scanner(System.in);

  
        num = scan.nextInt();
        max = num;
        min = num;

        while( num != 0)// using all numbers your program reads (including the last number 0), you need to compute the minimum
        {
            if(num> MAX)
                num = MAX;

            if(num > max)
                max = num;

            if(num < min)
                min = num;

            if (num < 0)// sum of only negative integers (integers that are less than 0)
                 sum+= num;

            if (num%2 != 0  )  //sum of odd integers (integers that cannot be divided by 2, you can use "num%2 != 0")
                odd+=num;

            if(num > 0) //  count how many positive integers (integers that are greater than 0) in the sequence.
            count ++;

             num = scan.nextInt();
        }




         System.out.println("The minimum integer is " + min);
         System.out.println("The sum of the negative integers is " + sum);
         System.out.println("The sum of the odd integers is " + odd);
         System.out.println("The number of positive integers in the sequence is " + count);

    }
}

??


Expected Output:

The Minimun Integer is 0.
The sum of the negative integers is 0.
The sum of the odd integers is 0
The number of positive integers in the sequence is 2

Input is 4 2 0 1 3.

Minimun integer is 0 : good coz 0 is the minimum.

Sum of negative numbers is 0 : good coz there are no negative numbers

Sum of odd integers is 0: eh?? 1 and 3 are odd integers so their sum should be 4.

Number of positive integers in the sequence is 2: eh?? 4 2 1 3 are positive integers. 0 is neither positive nor negative. the number of positive integers in the sequence should be 4.

I fixed the code. Need a do while loop. heres the code

public class Assignment2
{
  public static void main (String[] args)
  {

        int num, odd=0 ,sum=0,count = 0;
        int min = 2147483647;//Maximun number one can input
        Scanner scan = new Scanner(System.in);





        [B]do
         {
num = scan.nextInt();

            if(num < min)
                min = num;

            if (num < 0)// sum of only negative integers (integers that are less than 0)
                 sum+= num;

            if (num%2 != 0  )  //sum of odd integers (integers that cannot be divided by 2, you can use "num%2 != 0")
                odd+=num;

            if(num > 0) //  count how many positive integers (integers that are greater than 0) in the sequence.
            count ++;


        }
 while( num != 0);// using all numbers your program reads (including the last number 0), you need to compute the minimum[/B]




         System.out.println("The minimum integer is " + min);
         System.out.println("The sum of the negative integers is " + sum);
         System.out.println("The sum of the odd integers is " + odd);
         System.out.println("The number of positive integers in the sequence is " + count);

    }
}

Edited 5 Years Ago by Lxyslckr: n/a

This question has already been answered. Start a new discussion instead.