Results 1 to 4 of 4
Thread: Counting number of user input
- 10-24-2012, 09:07 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 5
- Rep Power
- 0
Counting number of user input
My program reads input from a user until '0' is entered. How do I count the number of inputs given by the user? My code gives '0' whenever I ask for it.
Java Code:import java.util.Scanner; public class NumberLab3 { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print ("Please enter a positive number (0 to terminate): "); int count; count = input.nextInt(); while (count != 0) { if (count != 0) System.out.print ("Please enter another number (0 to terminate): "); count++; count = input.nextInt(); } if (count == 0); System.out.println ("Program terminated."); System.out.println ("Number of numbers entered: " + count); } }Last edited by cs3; 10-24-2012 at 09:27 AM.
- 10-24-2012, 09:11 AM #2
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
Re: Counting number of user input
You're using count for two things, storing the numbers typed by the user and storing the amount of times it went through the loop.
Make a second variable.
- 10-24-2012, 09:23 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 5
- Rep Power
- 0
Re: Counting number of user input
How exactly would the second variable operate? I don't know how I'd make it count the number of user inputs
- 10-24-2012, 09:33 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Display occurrence of number based off user input… array
By Jason in forum New To JavaReplies: 14Last Post: 09-12-2011, 11:54 PM -
Random Number Generator with User Input
By person287 in forum New To JavaReplies: 14Last Post: 07-10-2011, 10:49 AM -
Counting number of entries in a file
By lenny0 in forum New To JavaReplies: 3Last Post: 05-10-2011, 03:40 AM -
Creating a number of objects based on user input.
By Kevinius in forum New To JavaReplies: 21Last Post: 04-03-2011, 08:53 AM -
geting user input (number)
By kliane in forum New To JavaReplies: 9Last Post: 01-17-2010, 12:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks