Results 1 to 2 of 2
  1. #1
    cs3
    cs3 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Counting positive number inputs and negative number inputs of user. HELP!

    My program reads a number input of the user until '0' is given. My code is fine until I ask the program to count how many times they input a positive number and how many times they input a negative number.
    Java Code:
    import java.util.Scanner;
    public class NumberLab4 
    {
    	public static void main(String[] args) 
    	{
    		Scanner input = new Scanner (System.in);
    		System.out.print ("Please enter a number (0 to terminate): ");
    		
    		int count, numofinput = 0;
    		count = input.nextInt();
    				
    		while (count != 0) 
    		{
    			if (count != 0)
    				System.out.print ("Please enter another number (0 to terminate): ");
    					numofinput++;
    					count = input.nextInt();		
    		}
    		if (count == 0);
    			System.out.println ("Program terminated.");
    		
    		if (numofinput > 0)
    				System.out.println ("Number of positive numbers entered: " + numofinput);
    	
    		if (numofinput < 0)
    				System.out.println ("Number of negative numbers entered: " + numofinput);
    	
    			System.out.println ();
    		
    	}			
    }

  2. #2
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Counting positive number inputs and negative number inputs of user. HELP!

    The problem is because you're not differentiating between negative and positive numbers.

    Something like this
    Java Code:
    import java.util.Scanner;
    public class NumberLab4 
    {
        public static void main(String[] args) 
        {
            Scanner input = new Scanner (System.in);
            System.out.print ("Please enter a number (0 to terminate): ");
             
            int inputNr, negNr = 0, posNr = 0;
            inputNr = input.nextInt();
                     
            while (inputNr != 0) 
            {
                if (inputNr > 0)
                    posNr++;
                else
                    negNr++;
    
                System.out.print ("Please enter another number (0 to terminate): ");
                input = input.nextInt();        
            }
                System.out.println ("Program terminated.");
             
                System.out.println ("Number of positive numbers entered: " + posNr);
         
                System.out.println ("Number of negative numbers entered: " + negNr);
             
        }           
    }

Similar Threads

  1. Replies: 26
    Last Post: 12-03-2013, 02:55 PM
  2. Counting number of user input
    By cs3 in forum New To Java
    Replies: 3
    Last Post: 10-24-2012, 09:33 AM
  3. Replies: 3
    Last Post: 09-06-2012, 05:30 PM
  4. Replies: 23
    Last Post: 11-20-2011, 09:07 PM
  5. Sum positive numbers using 10 inputs from user
    By pvictory1 in forum New To Java
    Replies: 15
    Last Post: 10-10-2010, 01:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •