Results 1 to 3 of 3

Thread: breaking up

  1. #1
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default breaking up

    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
      }
    }
    inner loop: i need my code to compute the average for the 2nd, 3rd.. bowler independently to look like this:

    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    On this you've pointed above post.

    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
    			}
    Calculate the average from outside the loop, and calculate the total inside that.

Similar Threads

  1. breaking out of while loop
    By mac in forum New To Java
    Replies: 5
    Last Post: 05-18-2010, 03:21 PM
  2. Breaking up of array
    By agarwal_srushti in forum New To Java
    Replies: 3
    Last Post: 09-27-2009, 07:03 PM
  3. Breaking for-loops with listeners?
    By CBarry in forum New To Java
    Replies: 3
    Last Post: 04-22-2009, 03:38 AM
  4. Breaking down an integer
    By Emily in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 06:39 PM
  5. Breaking from nested switch
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 02-02-2008, 08:28 AM

Posting Permissions

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