View Single Post
  #1 (permalink)  
Old 01-07-2008, 07:43 AM
Torque Torque is offline
Member
 
Join Date: Jan 2008
Posts: 1
Torque is on a distinguished road
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.

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]); } } }
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:

Code:
public static void whichPlayer()//returns player index { }
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.

Then, he is given a menu that looks like this.

Quote:
***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
Here, I am focusing on the display scores methods, so let's pick number 1. This brings us to the scores menu.

Quote:
***Show Scores menu***
Enter your choice 1-2
1: Display the scores for a single player
2: Display the scores for all players
Alright lets test option number 2, the showAllScores method.

Quote:
2
Player 1 Round 1: 1
Player 2 Round 1: 2
Works like a charm! However I'm having problems with displaying the scores for only a certain player.

Okay so this time I pick option 1, and tell the program I want the players for player 1.

Quote:
Show scores for which player?
1
Player 1 Round 1: 1
Woah it works! Alright now lets try getting the scores for player 2.

Quote:
Show scores for which player?
2
Player 1 Round 1: 1


Here's all of it if you want to see it.

Quote:
----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: 1
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.

Last edited by Torque : 01-07-2008 at 07:47 AM.
Reply With Quote
Sponsored Links