Hi, i'm getting really frustrated with my code... as soon as a solve one problem another one comes up. Two problems arose in my code that I've realized are a result of the same problem. I have the user enter the amount of players in a bowling game. Then an array of objects is created for each player. If the user enters 3 for the amount of players, my array has index 0 - 2 as I expected but It asks them for the name of the user starting on index[1] instead of index[0].
System.out.println("How many players are playing?"); numberOfPlayers = (input.nextInt() + 1); bowlingclass names[] = new bowlingclass[numberOfPlayers]; System.out.println("Enter the name of each player."); for (int i = 0; i < numberOfPlayers; i++){ name = input.nextLine(); names[i] = new bowlingclass(name); //Objects created for each player }
On the next block of code is another for loop where the actual bowling game takes place.
do { frames++; for (int i = 0; i < numberOfPlayers; i++){ System.out.println("It is "+names[i].name+"'s turn to throw."); System.out.println(names[i].name+" throws his ball..."); try { Thread.currentThread().sleep(3000); } catch ( Exception e ) { } System.out.println(names[i].name+" hits ") ; } } while (frames <= 10); } }
Each iteration of the for loop is a player's turn to bowl. Lets say Joe and Bob are the two names that were entered. The output this code is
It is 's turn to throw. throws his ball... hits 10 bowling pins! It is bob's turn to throw. bob throws his ball... bob hits 10 bowling pins! It is joe's turn to throw. joe throws his ball... joe hits 10 bowling pins! It is 's turn to throw. throws his ball... hits 10 bowling pins!
So it looks like when I assign a name for the three objects, it skips index[0] and starts at index[1].
This is probably a careless mistake somewhere, but I have tried to fix it for over an hour now and I'm not sure what is wrong. Thank you for any help!