Results 1 to 3 of 3
  1. #1
    Torque is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default 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.

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

    ***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.

    ***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.

    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.

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

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

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

    ----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 06:47 AM.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Unhappy 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:
    Java Code:
    round	player	score
    0	0	12
    	1	31
    	2	45
    		
    1	0	12
    	1	48
    	2	67
    This means that every round is an array of players.
    I would rather define it so that every player is an array of rounds.
    Java Code:
    player	round	score
    0	0	12
    	1	12
    1	0	31
    	1	48
    2	0	45
    	1	67
    So, your array would be defined:
    Java Code:
    int[][]strokes =new int[numberOfRounds][numberOfPlayers];
    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. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

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

  1. need help with my 1st multi-method program
    By Phobos0001 in forum New To Java
    Replies: 6
    Last Post: 02-08-2008, 06:44 AM
  2. Taking Java In School Need Help
    By xEuPhOrIcSx in forum New To Java
    Replies: 7
    Last Post: 02-04-2008, 09:02 AM
  3. Beginner Needs Help w/ Program for School
    By badness in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 08:51 PM
  4. Please help... assignment for school
    By confused2000 in forum New To Java
    Replies: 3
    Last Post: 11-12-2007, 09:12 AM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •