0

Basically the output will tell the minimum integer, sum of odd integers, and count of negative integers when the user inputs a list of numbers (Stops when user enters 0). I get the count of negative numbers correct but the problem I am encountering is that the sum of odd integers only adds when numbers are positive and the minimum integer always turns out 0. This is my code

package example2;
import java.util.Scanner;
public class ex {

public static void main(String[] args)
{ Scanner sc = new Scanner(System.in);
int oddsum=0, min=0, negcount=0;
int number;
do
{

number=sc.nextInt();

    if(number< 0)
     {

        negcount++;

     }
    else if(number%2!=0)
     {
         oddsum+=number;
     }
    else if(number<min)
     {
         number=min;
    }

}while(number!=0);
 System.out.println("The minimum integer is "+min);
 System.out.println("The sum of odd integers is "+oddsum);
 System.out.println("The count of negative integers in the sequence is "+negcount);


}
}
J.Godinez
  • 49
  • 3
  • you dont need `if else if` do a simple three if and that would resolve your issue – SMA Oct 30 '16 at 07:12
  • for the minimum: it is never a good idea to initialize `min` with `0`. In your example: if the user input is `< 0`, you enter the first `if`. But this means you do not enter the `else if` at the end. So the input must be `>= 0` to enter the last `else if`, but then it cannot be `< 0`. Initialize `min` with `Integer.MAX_VALUE` and remove both `else`'s (but not the corresponding `if`). – Turing85 Oct 30 '16 at 07:14
  • I tried that. When I enter a negative number the program stops – J.Godinez Oct 30 '16 at 07:16
  • @J.Godinez yes, because your assignment is screwed up: `number = min` should be `min = number` – Turing85 Oct 30 '16 at 07:17
  • As presented, this does not specify whether or not the terminating input or zero shall be processed. (While `0 <= min`, you only need to check `number<0` if `number – greybeard Oct 30 '16 at 08:31

2 Answers2

0

You're using an if-else construct, meaning that only one of the branches will be executed. Instead, you should use separate if statements and evaluate each condition separately:

if (number< 0) {
    negcount++;
}

if (number % 2 != 0) {
    oddsum += number;
}

if (number < min) {
    min = number;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • two remarks: `min` should be initialized with `Integer.MAX_VALUE` and the assignment in the last if is the wrong way around, it should be `min = number`. – Turing85 Oct 30 '16 at 07:18
  • Awww Gotcha. Dumb mistake. Got the outputs right now thanks. – J.Godinez Oct 30 '16 at 07:21
0
package example2; 
import java.util.Scanner; 
public class ex { public static void 
main(String[] args)
{ 
Scanner sc = new     
Scanner(System.in);
int oddsum=0, min=0, negcount=0; 
int number; 
do 
{
number=sc.nextInt(); 
if(number< 0) 
{ 
negcount++; 
}
if(number%2!=0)
{
 oddsum+=number;
}
if(number<min) 
{ 
min=number; 
} 
}while(number!=0); 

System.out.println("The minimum 
integer is "+min);     

System.out.println("The sum of odd
integers is "+oddsum);   

System.out.println("The count of    
negative integers in the sequence is
"+negcount); 
}
}

Also refer How does java do modulus calculations with negative numbers?

Community
  • 1
  • 1
Kunal Parikh
  • 463
  • 4
  • 15