Results 1 to 3 of 3
Thread: breaking up
- 10-31-2010, 03:33 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
breaking up
inner loop: i need my code to compute the average for the 2nd, 3rd.. bowler independently to look like this:Java Code:import javax.swing.JOptionPane; import java.util.Scanner; import java.text.*; public class bowlingteam { public static void main (String[]args) { Scanner input = new Scanner (System.in); DecimalFormat formatIt = new DecimalFormat("#.#"); int score = 0; int playerTotal = 0; double playerAvg = 0.0; int numberOfGames = 3; String output = "Your scores and average are:\n"; int team_total = 0; int team_average = 0; //Sample structure for Team //outer loop for (int j = 0; j<5; j++) { //inner loop for (int i =0; i< numberOfGames; i++) // 0, 1, 2 (3 games for each bowler) { System.out.print("Please enter your score: ");//prompt for score score = input.nextInt(); //read in score playerTotal = score;//accumulate to get total_score playerAvg += playerTotal/numberOfGames;//divide total_score by 3 to get average score for each player } JOptionPane.showMessageDialog(null,"Your average is: " + playerAvg);//output each player’s average } team_total += playerTotal; //accumulate sum of each players total_score to get a team_total team_average = team_total/15;//Divide team_total by 15 to get team_average JOptionPane.showMessageDialog(null, "The team average is: "+ formatIt.format(team_average)); //Output team_average } }
enter scorce: 100
enter scorce: 200
enter score: 0
output average for player1
enter scorce: 100
enter scorce: 200
enter score: 155
output average for player2
and soo on.... to player 5
the average for the whole team is: team average.
right now, my accummilator continuously adds all of the scores together for all players. what can i do to fix this? i hope this makes sense.
-
You need to have the player's total be intialized to zero before you add the player's score. So do that in the outer for loop before the individual player's for loop.
Also, you'll need to calculate the player's average after the inner (player's) for loop has been completed because only then will the individual scores have been all added up.
Oh, please fix your indentation formatting since it is atrocious and makes your code quite difficult to read. If you're a student, bad formatting will likely get marks taken off of your grade.
- 10-31-2010, 07:58 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
On this you've pointed above post.
Calculate the average from outside the loop, and calculate the total inside that.Java Code:for (int i =0; i< numberOfGames; i++) { // 0, 1, 2 (3 games for each bowler) System.out.print("Please enter your score: ");//prompt for score score = input.nextInt(); //read in score playerTotal = score;//accumulate to get total_score playerAvg += playerTotal/numberOfGames;//divide total_score by 3 to get average score for each player }
Similar Threads
-
breaking out of while loop
By mac in forum New To JavaReplies: 5Last Post: 05-18-2010, 03:21 PM -
Breaking up of array
By agarwal_srushti in forum New To JavaReplies: 3Last Post: 09-27-2009, 07:03 PM -
Breaking for-loops with listeners?
By CBarry in forum New To JavaReplies: 3Last Post: 04-22-2009, 03:38 AM -
Breaking down an integer
By Emily in forum New To JavaReplies: 1Last Post: 03-06-2008, 06:39 PM -
Breaking from nested switch
By javaplus in forum New To JavaReplies: 3Last Post: 02-02-2008, 08:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks