Results 1 to 3 of 3
- 01-13-2011, 03:19 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 40
- Rep Power
- 0
Making a loop and creating variables based off of user input
Ok I'm just wondering how I would right a piece of code that will take user input and based off the integer entered knows how many variables to store. I sorta got stuck when reading my code and realized that all the variables would get stored in the same place and erased the previous int. Here's what I have....
I know this code is not the best and it isn't finished but I'm working on it and I have a test for my CS class and I would like to figure this one out so I can continue. Thanks everyone.Java Code:public class AgeTest { /** * @param args */ public static void main(String[] args) { Scanner input = new Scanner; int age; // The age that was entered by user int control = 0; // The control variable int amountusr; // The amount of users at the time that will be entering ages System.out.println("Hello, how many users will be entering ages today?\n"); // Output this text amountusr = input.nextInt() amountusr = control; int first; int second; int third; int fourth; int fifth; int sixth; int seventh; int eight; int ninth; int tenth; while (control != 0) { System.out.println("What is your age?\n"); first = input.nextInt() control--; } } }
- 01-13-2011, 03:22 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
create an int[] and make it have size input.nextInt().
Then loop through that array, for each i, prompt user to enter there age and add that input to position i in the array
- 01-13-2011, 05:43 AM #3
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
Yes as sunde887 said use an array.
Arrays are very fun :) and would be perfect for what you are asking.
So a user inputs 10 for "amount of users". Now amountusr = 10. Now you can immediately create an array called users (or whatever name). And in java you need to declare an array like this.
When you find out the amount of users you can add it like this.Java Code:int[] users;
or the same thing.Java Code:users = new int[amountusr]; //or users = new int[19];
You can store integers to this array like thisJava Code:int[] users = new int[amountusr];
so this is really cool when you use loops with them and this is what you are needing to do.Java Code:users[0] = 35; users[1] = 25; users[2] = 52; users[3] = 16;
I would try a for loop where your while loop is. You will need the array length and you can get an array length like this.
You will need to start at 0 so have your for loop go from 0 to user.length.Java Code:users.length;
if "i" is your for loop integer variable name then it might look like this.
Java Code:for (int i = 0; i < users.length; i++) { users[i] = input.nextInt(); }Last edited by AcousticBruce; 01-13-2011 at 05:46 AM.
Similar Threads
-
Scanner user input not working in conjunction with while loop.
By TheNadz in forum New To JavaReplies: 5Last Post: 11-27-2010, 03:49 AM -
Creating a Table with user input
By JonniBravo in forum EclipseReplies: 1Last Post: 09-08-2010, 12:50 PM -
[SOLVED] User Input - loop
By new person in forum New To JavaReplies: 4Last Post: 02-22-2009, 10:02 PM -
loop when there is no user-input
By becky in forum New To JavaReplies: 12Last Post: 02-02-2009, 10:02 PM -
Making arrays by reading user input
By apfroggy0408 in forum New To JavaReplies: 23Last Post: 04-30-2008, 01:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks