Salutations ladies and gentlemen! This seems like a helpful forum and I'm very glad it exists. This is my first post here but I've read the rules plus Common Java Mistakes and "Helloworld922's Java Tips." It seems like there are some very knowledgeable people here, and the majority of the advice is way beyond me. XD I've tried to look for similar problems but have not found any, and I have a class to get to shortly. I apologize if this is a redundant thread.
I'm taking Computer Programming I which centers on Java but it's the professor's first year teaching and I'm trying to stay ahead of the class. We just did "Hello World" a week ago or so. Therefore while I am in class this is not a class problem so there's no academic dishonesty concerns.
I am trying to create a simple game that asks for a number of players and then creates that many Player objects. I'll try to remember and type the while loop I used, however I do not have my Java textbook with me and the computers in this wing of the school don't have a Java text editor so I apologize for the inevitable errors.
System.out.println("How many players?"); Scanner scan = new Scanner(); int storage = scan.nextInt(); int counter = 1; while(counter <= storage) { Player player = new Player(counter); System.out.println("Welcome Player" + counter + " !"); counter++; }
Now this prints out what I want but I believe instead of creating multiple Players it just overwrites the same Player repeatedly. I've thought of 2 ways to fix this:
Brute Force Method: Instead of a while loop create a if/elseif for every possible int value. This seems like a bad idea to me.
Way I want it to work Method: "Player (player+counter) = new Player(counter);" , where (player+counter) becomes player1 for the first loop, player2 for the second, etc. The problem with this is I don't think it's possible. Does the functionality exist to have a variable in the instantiation of an object like that?
Again I'm not being paid for this and it's not a school assignment, but I'd like to figure these things out ahead of time so I can help out other people in class if I need too (we have a couple of students in class that don't speak English well and one sits next to me and when he asks me for help and I don't understand or can't help him I feel really bad).
Thanks in advance for any assistance. I really appreciate the fact a forum like this exists.
Edit: Thanks again to KevinWorkman! I tried to edit a [Solved] tag to this but can't seem to find the option in the Edit menu.