Results 1 to 5 of 5
Thread: Java Beginner needs help!!
- 11-22-2008, 10:52 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
Java Beginner needs help!!
For My computer science class I have to create a program on Ready to Program Java that takes in a basketball team and shows the user:
1) Enter in a player : First Name, Last Name
2) Enter in the statistics for a player: average rebounds per game, average points per game, average assists per game
3) Search for a player: Search by their name, and find all the relevant statistics
4) Search by category: Find the Statistical leader for a given category
5) Update a player's statistics
6) Compare two players: who has the higher average for a given statistical category
7) Compare two players: who is the overall better player (how will you figure this out?)
SO far I have this:
Number 1 works for me but i have no clue how the user can update the players statistics. I know I have to use arrays to store in the stats for each player but i dont know how to take in the numbers in the array and let the user change them, allowing them to update the array. I also cannot figure out how to find the statistical leader for a given category like assists or rebounds.XML Code:c.println ("Welcome,We are the Lakers"); //print a welcome for the user which explains what team we are doing c.println("");//add a space inbetween while (true)//add a while loop to brng up the menu again after the user is done with one option { c.println ("Menu");//print menu to the screen to show String[] Options = new String [5]; //make an array //array shows the options the user can choose from Options [0] = "1) Player Stats"; //shows Player stats as users first option Options [1] = "2) Stats Leader"; //shows stats leader as users second option Options [2] = "3) Update Player Stats"; //shows update player stats as users second option Options [3] = "4) Compare Two Players"; //shows compare two players as users third option Options [4] = "5) Main Menu"; //shows menu as users fourth option c.println (""); //add a space in between // print the options on the screen c.println (Options [0]); //print first option to the screen c.println (Options [1]); //print second option to the screen c.println (Options [2]); //print third option to the screen c.println (Options [3]); //print fourth option to the screen c.println (Options [4]); //print fifth option to the screen c.println (""); //add a space inbetween c.println ("Please type in the corresponding number according to the options"); //ask the user to enter in the number for what option they want String[] Rebounds = new String [12]; //make an array for rebounds for each player Rebounds [0] = "4.90"; //save 4.90 as trevor arizas rebounds Rebounds [1] = "5.10"; //save 5.10 as kobe bryant rebounds Rebounds [2] = "8.90"; //save 8.90 as andrew bynum rebounds Rebounds [3] = "3.40"; //save 3.40 as Jordan Farmer rebounds Rebounds [4] = "2.60"; //save 2.60 as Derek Fisher rebounds Rebounds [5] = "10.10"; // save 10.10 as Pau Gasol rebounds Rebounds [6] = "1.30"; // save 1.30 as Chris Mihm rebounds Rebounds [7] = "5.90"; // save 5.90 as Lamar Odom rebounds Rebounds [8] = "1.80"; // save 1.80 as Josh Powell rebounds Rebounds [9] = "3.30"; // save 3.30 as Vladimir Radmanovic rebounds Rebounds [10] = "1.90"; // save 1.90 as Sasha Vujacic rebounds Rebounds [11] = ".40"; // save .40 as Luke Walton rebounds String[] Points = new String [12]; //make an array that shows the points for each player Points [0] = "9.4"; //save 9.4 as trevor arizas points Points [1] = "24.4"; //save 24.4 as kobe bryant points Points [2] = "9.2"; //save 9.2 as andrew bynum points Points [3] = "10.2"; //save 10.2 as Jordan Farmer points Points [4] = "10.6"; //save 10.6 as Derek Fisher points Points [5] = "17.6"; // save 17.6 as Pau Gasol points Points [6] = "4.7"; // save 4.7 as Chris Mihm points Points [7] = "9.3"; // save 9.3 as Lamar Odom points Points [8] = "2.8"; // save 2.8 as Josh Powell points Points [9] = "6.1"; // save 6.1 as Vladimir Radmanovic points Points [10] = "5.2"; // save 5.2 as Sasha Vujacic points Points [11] = ".6"; // save .6 as Luke Walton points String[] Assists = new String [12]; //make an array that shows the assists for each player Assists [0] = "1.7"; //save 1.7 as trevor arizas assists Assists [1] = "4.0"; //save 4.0 as kobe bryant assists Assists [2] = "1.8"; //save 1.8 as andrew bynum assists Assists [3] = "2.9"; //save 2.9 as Jordan Farmer assists Assists [4] = "2.7"; //save 2.7 as Derek Fisher assists Assists [5] = "3.1"; // save 3.1 as Pau Gasol assists Assists [6] = ".0"; // save .0 as Chris Mihm assists Assists [7] = "2.2"; // save 2.2 as Lamar Odom assists Assists [8] = ".0"; // save .0 as Josh Powell assists Assists [9] = "1.1"; // save 1.1 as Vladimir Radmanovic assists Assists [10] = "1.4"; // save 1.4 as Sasha Vujacic assists Assists [11] = ".6"; // save .6 as Luke Walton assists int useroptions = c.readInt (); //take in what the user chose as their option if (useroptions == 1) //if the options equal 1 { c.println ("You have chosen the Player Stats Option"); //print this to the screen c.println (""); //add space inbetween c.println ("Please type in the corresponding number of the player to see their stats:"); //ask the user to type in the number of the player they want to see the stats for String[] FirstNames = new String [12]; //get an array for the names of the players FirstNames [0] = "1) Trevor Ariza"; //save trevor ariza as first name in the array FirstNames [1] = "2) Kobe Bryant"; //save kobe bryant as second name in the array FirstNames [2] = "3) Andrew Bynum"; //save andrew bynum as third name in the array FirstNames [3] = "4) Jordan Farmer"; //save jordan farmer as the fourth name in the array FirstNames [4] = "5) Derek Fisher"; //save derek fisher as the fifth name in the array FirstNames [5] = "6) Pau Gasol"; //save pau gasol as the sixth name in the array FirstNames [6] = "7) Chris Mihm"; //save chris mihm as the seventh name in the array FirstNames [7] = "8) Lamar Odom"; //save lamar odom as the eigth name in the array FirstNames [8] = "9) Josh Powell"; //save josh powell as the ninth name in the array FirstNames [9] = "10) Vladimir Radmanovic"; //save Vladimir Radmanovic as the tenth name in the array FirstNames [10] = "11) Sasha Vujacic"; //save sasha vujacic as the eleventh name in the array FirstNames [11] = "12) Luke Walton"; //save luke walton as the twelvth name in the array c.println (FirstNames [0]); //print the trevor ariza to the screen c.println (FirstNames [1]); //print the kobe bryant to the screen c.println (FirstNames [2]); //print the Andrew Bynum to the screen c.println (FirstNames [3]); //print the Jordan Farmer to the screen c.println (FirstNames [4]); //print the Derek Fisher to the screen c.println (FirstNames [5]); //print the Pau Gasol to the screen c.println (FirstNames [6]); //print the Chris Mihm to the screen c.println (FirstNames [7]); //print the Lamar Odom to the screen c.println (FirstNames [8]); //print the Josh Powell to the screen c.println (FirstNames [9]); //print the Vladimir Radmanovic to the screen c.println (FirstNames [10]); //print the Sasha Vujacic to the screen c.println (FirstNames [11]); //print the Luke Walton to the screen c.println (""); //add space inbetween int userschoseplayer = c.readInt (); // declares userschoseplayer as an integer, also reads the integer if (userschoseplayer == 1) // if userchoseplayer equals 1 { c.println ("You have picked Trevor Ariza, His stats are: Rebounds=4.90, Assists=1.7, Points=9.4"); // outputs Trevor Ariza's rebounds, assists and points to the screen } if (userschoseplayer == 2) //if userchoseplayer equals 2 { c.println ("You have picked Kobe Bryant, His stats are: Rebounds=5.10, Assists=4.0, Points=24.4"); // outputs Kobe Bryant's rebounds, assists and points to the screen } if (userschoseplayer == 3) //if userchoseplayer equals 3 { c.println ("You have picked Andrew Bynum, His stats are: Rebounds=8.90, Assists=1.8, Points=9.2"); // outputs Andrew Bynum's rebounds, assists and points to the screen } if (userschoseplayer == 4) //if userchoseplayer equals 4 { c.println ("You have picked Jordan Farmer, His stats are: Rebounds=3.40, Assists=2.9, Points=8.6"); // outputs JOrdan Farmer's rebounds, assists and points to the screen } if (userschoseplayer == 5) //if userchoseplayer equals 5 { c.println ("You have picked Derek Fisher, His stats are: Rebounds=2.60, Assists=2.7, Points=10.6"); // outputs Derek FIsher's rebounds, assists and points to the screen } if (userschoseplayer == 6) //if userchoseplayer equals 6 { c.println ("You have picked Pau Gasol, His stats are: Rebounds=10.10, Assists=3.1, Points=17.6"); // outputs Pau Gasol's rebounds, assists and points to the screen } if (userschoseplayer == 7) //if userchoseplayer equals 7 { c.println ("You have picked Chris Mihm, His stats are: Rebounds=1.30, Assists=.0, Points=4.7"); // outputs Chris Mihm's rebounds, assists and points to the screen } if (userschoseplayer == 8) //if userchoseplayer equals 8 { c.println ("You have picked Lamar Odom, His stats are: Rebounds=5.90, Assists=2.2, Points=9.3"); // outputs Lamar Odom's rebounds, assists and points to the screen } if (userschoseplayer == 9) //if userchoseplayer equals 9 { c.println ("You have picked Josh Powell, His stats are: Rebounds=1.80, Assists=.0, Points=2.8"); // outputs Josh Powell's rebounds, assists and points to the screen } if (userschoseplayer == 10) //if userchoseplayer equals 10 { c.println ("You have picked Vladimir Radmanovic, His stats are: Rebounds=3.30, Assists=1.1, Points=6.1"); // outputs Vladimir Radmanovic's rebounds, assists and points to the screen } if (userschoseplayer == 11) //if userchoseplayer equals 11 { c.println ("You have picked Sasha Vujacic, His stats are: Rebounds=1.90, Assists=1.4, Points=5.2"); // outputs Sasha Vujacic's rebounds, assists and points to the screen } if (userschoseplayer == 12) //if userchoseplayer equals 12 { c.println ("You have picked Luke Walton, His stats are: Rebounds=.40, Assists=.6, Points=.6"); // outputs Luke Walton's rebounds, assists and points to the screen } } if (useroptions == 2) //if the user chooses 2 { c.println ("You have chosen the Statistical Leader option."); //tells user that they have chosen Statistical leader as their options c.println (""); //add a space inbetween String[] Stats = new String [3]; //add a new array that shows the user the options Stats [0] = "1) Most Rebound"; // save most rebounds as the first user option Stats [1] = "2) Most Assists"; //save most assists as the second user option Stats [2] = "3) Most Points"; //save most points as the third user option c.println (Stats [0]); //print most rebound to the screen c.println (Stats [1]); //print most assists to the screen c.println (Stats [2]); //print most points to the screen c.println (""); //add a space to the screen c.println ("Please type in a corresponding number according to the options"); //ask the user to to type in a number depending on what option they want int userchosenstats = c.readInt (); //take in an integer from the user and read it if (userchosenstats == 1) //if the user chooses the first option { c.println ("The player that has the most Rebounds is:"); //show the player that has the most rebounds } if (userchosenstats == 2) //if the user chooses the second option { c.println ("The player that has the most Assists is:"); //show the player that has the most assists } if (userchosenstats == 3) // if the user chooses the third option { c.println ("The player that has the most Points is:"); //show the player that has the most points } } if (useroptions == 3) //if the user chooses the third option { c.println ("You have chosen the Update option."); //print that they have chosen the update option c.println (""); //add a space inbetween } if (useroptions == 4) //if the user chooses the fourth option { c.println ("You have chosen to Compare two players."); //print that they have chosen the update option } }
-
I see that you have a huge bunch of if's that ignore the data in your arrays, and hard-codes what the output should be based on which number was pressed. What if the data arrays are changed? Why even bother having them.
What I'm driving at is that you should extract information from your data as if you have no knowledge of what the data holds. Let the data arrays do the hard work for you. If the user presses "2" you convert that into an int and display the output with a simple bit of code that uses FirstNames[2], assists[2], etc...
Also, please know that there is a short-cut for declaring arrays that will make your code a bit more readable.
Java Code:public class Fubar { public static void main(String[] args) { String[] myStrings = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }; for (int i = 0; i < myStrings.length; i++) { System.out.println(myStrings[i]); } } }
- 11-22-2008, 11:47 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
How??
How will i do that though?? Can you put that in the code to show me what you are getting at?? Sorry, I am just a beginner, I am only in grade 11 and this is my first time with java. By fixing the arrays will that solve all the problems?? Because the thing I am having the most problem with is allowing the user to update the stats of a palyer.:confused:
- 11-23-2008, 01:15 AM #4
Short version is that they are called variables. Fubarable has already dug through the code so I will leave details to Fubarable.
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
-
One way to do this, not the most elegant, but perhaps the easiest at your stage is to use parallel array as you've set things up. In this situation, the data on each athlete is split among several arrays. The hard part about this is if you change the order of any array, you must do the exact same change in all arrays. Here's a simple example of what I mean:
When a choice is made and placed in the choice variable, it's used to get the necessary data from each array, i.e., studentNames[choice] and studentScores[choice]. Notice that I subtracted 1 from the choice since Java arrays don't start from 1, they start from 0 -- called 0-based arrays.Java Code:import java.util.Scanner; public class Fubar2 { static String[] studentNames = {"Abe", "Ben", "Charles"}; static int[] studentScores = {50, 75, 100}; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Choose a student from 1 to " + studentNames.length + ": "); int choice = scanner.nextInt() - 1; // simplified, no error trapping scanner.nextLine(); System.out.println("You chose student " + studentNames[choice] + " whose score is: " + studentScores[choice]); } }
Notice that I don't use if statements here but I directly get the info from the arrays.Last edited by Fubarable; 11-23-2008 at 02:14 AM.
Similar Threads
-
almost done...beginner needs help plz..
By shongo in forum New To JavaReplies: 15Last Post: 11-10-2008, 08:14 AM -
Beginner Java graphics - filling concentric circles with color
By GenkiSudo in forum New To JavaReplies: 4Last Post: 09-13-2008, 11:07 AM -
Help with Beginner exercise.
By Trunksten in forum New To JavaReplies: 1Last Post: 07-10-2008, 05:40 AM -
[SOLVED] Need help with Java Vocab for final (Beginner vocab)
By Zebra in forum New To JavaReplies: 9Last Post: 05-26-2008, 04:30 AM -
beginner to Java
By notwist in forum New To JavaReplies: 15Last Post: 04-18-2008, 09:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks