Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-07-2008, 06:43 AM
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 06:47 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-07-2008, 10:14 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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:
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.
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:
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-07-2008, 08:45 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Code:
switch(menuChoice) { /*case 1: showPlayerScores(tempArray); <-- uncomment this 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(); <-- your problem is from here on* 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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 02:09 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org