Results 1 to 2 of 2
- 10-24-2012, 10:03 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 5
- Rep Power
- 0
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 (); } }
- 10-24-2012, 12:21 PM #2
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
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
-
Counting number of user input
By cs3 in forum New To JavaReplies: 3Last Post: 10-24-2012, 09:33 AM -
get three inputs at one time, then perform the conversions on the three inputs!
By niloufar in forum New To JavaReplies: 3Last Post: 09-06-2012, 05:30 PM -
finding the power to a number then the user inputs a max a min power
By vicu1 in forum New To JavaReplies: 23Last Post: 11-20-2011, 09:07 PM -
Sum positive numbers using 10 inputs from user
By pvictory1 in forum New To JavaReplies: 15Last Post: 10-10-2010, 01:30 AM -
inputs numbers then outputs how many time a particular number appears
By koji_kun in forum New To JavaReplies: 23Last Post: 12-22-2009, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks