Segregating results per iteration of a loop

JusLip :

I've got an assignment which is to simulate a basketball free throw shooter's percentage of made shots across 5 games with 10 shots per game. For the most part the code is working how I want it to but for each game I need print out the number of in shots out of 10. I cannot figure out how to keep this result specific to the correlating game rather than increasing the in number across all games. Each following game is adding its in amount to the previous game's.

Then I also need help on calling out the best and worst scores based on the number of in shots for each game. I believe the two problems can be solved by the same action, I'm just having trouble finding what action that needs to be.

import java.util.*;

public class Final2 {
    public static void main(String[] args){
        double in;
        int out;
        int count;
        int games;
        int tries;
        double average;
        int total;

        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Player's Free Throw Percentage: ");
        int input = scan.nextInt();
        count = 0;
        in = 0;
        average = 0;
        total = 0;
        games = 1;

        do{             
            System.out.println("\nGame " + games + ":");
            games++;

            for (tries = 0; tries < 10; tries++){
                int shot = (int)(Math.random()*100);
                count++;

                if (shot < input){
                   in++;
                    System.out.print("IN ");
                }   

                else{
                    System.out.print("OUT ");
                }               
            }
            System.out.println("\nFree Throws Made: " + String.format("%.0f", in) + " Out Of 10. ");
        }   
        while (games <= 5);{
        }           
        average = (in / count)*100;

        System.out.println("\nSummary:");
        System.out.println("Best Game Free Throws Made: " + "...");
        System.out.println("Worst Game Free Throws Made: " + "...");
        System.out.println("Total Free Throws Made: " + String.format("%.0f", in) + " Out Of " + count);
        System.out.println("Average Free Throw Percentage: " + String.format("%.0f", average) + "%");       
        System.out.println("\nEND OF SIMULATION!");     
    }
}

Output:

Enter Player's Free Throw Percentage: 50

Game 1:
IN IN IN IN IN IN IN OUT OUT OUT 
Free Throws Made: 7 Out Of 10. 

Game 2:
OUT IN OUT OUT OUT IN OUT IN IN IN 
Free Throws Made: 12 Out Of 10. 

Game 3:
IN OUT IN IN IN OUT OUT OUT OUT OUT 
Free Throws Made: 16 Out Of 10. 

Game 4:
IN OUT OUT OUT IN OUT OUT OUT IN IN 
Free Throws Made: 20 Out Of 10. 

Game 5:
OUT OUT IN OUT OUT OUT IN IN IN IN 
Free Throws Made: 25 Out Of 10. 

Summary:
Best Game Free Throws Made: ...
Worst Game Free Throws Made: ...
Total Free Throws Made: 25 Out Of 50
Average Free Throw Percentage: 50%

END OF SIMULATION!
ANONIMO ACOGNONIMO :

You have to reset the in variable before the while. If you want to keep track of the total number of in you have to use a new variable (ex. total_in).

import java.util.*;

public class Final2 {

    public static void main(String[] args){
        int in = 0;
        double total_in = 0;
        int out;
        int count = 0;
        int games = 1;
        int tries;
        double average = 0;
        int total = 0;

            Scanner scan = new Scanner(System.in);
            System.out.print("Enter Player's Free Throw Percentage: ");
            int input = scan.nextInt();

            do{             
                System.out.println("\nGame " + games + ":");
                games++;

                for (tries = 0; tries < 10; tries++){
                    int shot = (int)(Math.random()*100);
                    count++;

                    if (shot < input){
                       in++;
                        System.out.print("IN ");
                    }   

                    else{
                        System.out.print("OUT ");
                    }               
                }
                System.out.println("\nFree Throws Made: " + String.format("%.0f", in) + " Out Of 10. ");
                total_in += in;
                in = 0;
            }   
            while (games <= 5);{
            }           
            average = (total_in / count)*100;

        System.out.println("\nSummary:");
        System.out.println("Best Game Free Throws Made: " + "...");
        System.out.println("Worst Game Free Throws Made: " + "...");
        System.out.println("Total Free Throws Made: " + String.format("%.0f", total_in) + " Out Of " + count);
        System.out.println("Average Free Throw Percentage: " + String.format("%.0f", average) + "%");       
        System.out.println("\nEND OF SIMULATION!");     
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=216188&siteId=1