Results 1 to 3 of 3
- 01-07-2008, 05:43 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 1
- Rep Power
- 0
Need help with a program for high school regarding multi-dimensional arrays.
Hello! I am new here and new to Java in general. A sophomore in high school, I was assigned a task in my AP Computer Science class and was wondering if I could have some help with it and maybe be walked through it if possible whenever I run into troubles.
I was also supposed to define this method to return player indexes to use in my other method definitions, but I am not sure how:Java Code:import java.util.*; import java.util.Arrays; public class GolfTournament { public static void main(String[ ] args) { Scanner keyboard = new Scanner(System.in); boolean doItAgain = true; int menuChoice; int numberOfPlayers = 0; int numberOfRounds = 0; int whichPlayer; System.out.println("Enter the number of players:"); numberOfPlayers=keyboard.nextInt(); System.out.println("Enter the number of rounds:"); numberOfRounds=keyboard.nextInt(); int[][]strokes =new int[numberOfPlayers][numberOfRounds]; getScoresFromUser(strokes); do { System.out.println("***Golf Tournament Menu***"); System.out.println("Enter your choice 1-5"); System.out.println("1: Display all scores"); System.out.println("2: Show averages"); System.out.println("3: Show best scores"); System.out.println("4: Show worst scores"); System.out.println("5: Show winners"); System.out.println("6: Quit"); menuChoice = keyboard.nextInt(); switch(menuChoice) { case 1: scoresMenu(strokes); break; // case 2: averagesMenu(strokes); // break; case 3: bestScoresMenu(strokes); break; // case 4: worstScores(scores); // break; // case 5: winnersMenu(scores); // break; // case 6: doItAgain = false; // break; default: System.out.println("Not a valid menu choice."); break; } }while (doItAgain); } //local static methods public static void getScoresFromUser(int[][] tempArray) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter in the scores for:"); System.out.println(); for(int p=0; p < tempArray.length; p++) { for(int r=0; r < tempArray[p].length; r++) { System.out.println("Player "+(p+1)+" Round "+(r+1)+":"); tempArray[p][r] = keyboard.nextInt(); } } } public static void scoresMenu(int[][] tempArray) { Scanner keyboard = new Scanner(System.in); System.out.println("***Show Scores menu***"); System.out.println("Enter your choice 1-2"); System.out.println("1: Display the scores for a single player"); System.out.println("2: Display the scores for all players"); int menuChoice = keyboard.nextInt(); switch(menuChoice) { /*case 1: showPlayerScores(tempArray); break;*/ case 2: showAllScores(tempArray); break; default: System.out.println("Not A valid choice."); break; } } public static void showAllScores(int[][] tempArray) { for(int p=0; p < tempArray.length; p++) { for(int r=0; r < tempArray[p].length; r++) { System.out.println("Player "+(p+1)+" Round "+(r+1)+": "+tempArray[p][r]); } } } public static void showPlayerScores(int[][] tempArray) { Scanner keyboard = new Scanner(System.in); System.out.println("Show scores for which player?"); int whichPlayer = keyboard.nextInt(); for(int p=0; p < tempArray[p].length; p++) { for(int r=0; r < tempArray[p].length; r++) { System.out.println("Player "+(p+1)+" Round "+(r+1)+": "+tempArray[p][r]); } } }
Alright so first, the user is asked for the number of players for the Golf Tournament. Then, he is asked for the number of rounds for each players, and the scores for each player for every round.Java Code:public static void whichPlayer()//returns player index { }
Then, he is given a menu that looks like this.
Here, I am focusing on the display scores methods, so let's pick number 1. This brings us to the scores menu.***Golf Tournament Menu***
Enter your choice 1-5
1: Display all scores
2: Show averages
3: Show best scores
4: Show worst scores
5: Show winners
6: Quit
Alright lets test option number 2, the showAllScores method.***Show Scores menu***
Enter your choice 1-2
1: Display the scores for a single player
2: Display the scores for all players
Works like a charm! However I'm having problems with displaying the scores for only a certain player.2
Player 1 Round 1: 1
Player 2 Round 1: 2
Okay so this time I pick option 1, and tell the program I want the players for player 1.
Woah it works! Alright now lets try getting the scores for player 2.Show scores for which player?
1
Player 1 Round 1: 1
:(Show scores for which player?
2
Player 1 Round 1: 1
Here's all of it if you want to see it.
Anyone know the problem? I was supposed to accomplish this using the whichPlayer method and its player index return, but I did so otherwise meaning all of it is incorrectly programmed. I'm just trying to get the method to show scores for a certain player to work. Help is appreciated, thanks.----jGRASP exec: java GolfTournament
Enter the number of players:
2
Enter the number of rounds:
1
Enter in the scores for:
Player 1 Round 1:
1
Player 2 Round 1:
2
***Golf Tournament Menu***
Enter your choice 1-5
1: Display all scores
2: Show averages
3: Show best scores
4: Show worst scores
5: Show winners
6: Quit
1
***Show Scores menu***
Enter your choice 1-2
1: Display the scores for a single player
2: Display the scores for all players
2
Player 1 Round 1: 1
Player 2 Round 1: 2
***Golf Tournament Menu***
Enter your choice 1-5
1: Display all scores
2: Show averages
3: Show best scores
4: Show worst scores
5: Show winners
6: Quit
1
***Show Scores menu***
Enter your choice 1-2
1: Display the scores for a single player
2: Display the scores for all players
1
Show scores for which player?
1
Player 1 Round 1: 1
***Golf Tournament Menu***
Enter your choice 1-5
1: Display all scores
2: Show averages
3: Show best scores
4: Show worst scores
5: Show winners
6: Quit
1
***Show Scores menu***
Enter your choice 1-2
1: Display the scores for a single player
2: Display the scores for all players
1
Show scores for which player?
2
Player 1 Round 1: 1Last edited by Torque; 01-07-2008 at 05:47 AM.
- 01-07-2008, 09:14 AM #2
Problematic structure
Hello Torque.
The problem is how you defined your data structure. It worked fine up to know (looks like it), but now that you need to find on element in your data structure, it will need more code.
Currently you defined your data structure according to this example:
This means that every round is an array of players.Java Code:round player score 0 0 12 1 31 2 45 1 0 12 1 48 2 67
I would rather define it so that every player is an array of rounds.
So, your array would be defined:Java Code:player round score 0 0 12 1 12 1 0 31 1 48 2 0 45 1 67
Always draw a picture of your data structure before you start. I'm sorry for the inconvenience. There might still be a way of fixing this. ;)Java Code:int[][]strokes =new int[numberOfRounds][numberOfPlayers];
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-07-2008, 07:45 PM #3
showPlayerScores is really no different from showAllScores, minus a few lines. You ask for which player and even read it in from the keyboard, but you don't put it to use. In essence, you're making a call to two methods which perform basically the same function, with the array. If I haven't been clear, post back.Java Code:switch(menuChoice) { /*case 1: showPlayerScores(tempArray); [B]<-- uncomment this[/B] break;*/ case 2: showAllScores(tempArray); break; default: System.out.println("Not A valid choice."); break; } } public static void showAllScores(int[][] tempArray) { for(int p=0; p < tempArray.length; p++) { for(int r=0; r < tempArray[p].length; r++) { System.out.println("Player "+(p+1)+" Round "+(r+1)+": "+tempArray[p][r]); } } } public static void showPlayerScores(int[][] tempArray) { Scanner keyboard = new Scanner(System.in); System.out.println("Show scores for which player?"); int whichPlayer = keyboard.nextInt(); [B]<-- your problem is from here on*[/B] for(int p=0; p < tempArray[p].length; p++) { for(int r=0; r < tempArray[p].length; r++) { System.out.println("Player "+(p+1)+" Round "+(r+1)+": "+tempArray[p][r]); } } }Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
Similar Threads
-
need help with my 1st multi-method program
By Phobos0001 in forum New To JavaReplies: 6Last Post: 02-08-2008, 05:44 AM -
Taking Java In School Need Help
By xEuPhOrIcSx in forum New To JavaReplies: 7Last Post: 02-04-2008, 08:02 AM -
Beginner Needs Help w/ Program for School
By badness in forum New To JavaReplies: 2Last Post: 11-24-2007, 07:51 PM -
Please help... assignment for school
By confused2000 in forum New To JavaReplies: 3Last Post: 11-12-2007, 08:12 AM -
Help with array multi-dimensional
By barney in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks