简体   繁体   中英

Min & Max results of a for loop in java

Simulating basketball free throw performance and percentage. The code works fine but in the print out summary at the end I need to report the max score and the min score out of the 5 games. The output just needs to say, "Best game free throws made:" + a number (max). Then I also need the same thing for the worst game score (min). I think I need to have an array created from the for loop results but am at a loss on how to get something like this functioning.

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: " + 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!");     
 }
}

不要想得那么复杂,作为不解决分配的帮助:考虑使用 min 和 max 变量。

Each time through the game loop check to see if the in is bigger than your "running max" ie maxShots and if in is less than your "running min" ie minShots.

Also be careful how you initialize these max and min variables. If you set minShots = 0, it might report that the minimum is 0, even if the actual minimum is more than that. This is because when you check to see if in is less than minShots, 0 will always always be lower than or equal to 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 maxShots = 0;
    int minShots = 10;
    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: " + in + " Out Of 10. ");
            total_in += in;
            if(in > maxShots){
                maxShots = in;
            }
            if(in < minShots){
                minShots = in;
            }
            in = 0;
        }   
        while (games <= 5);{
        }           
        average = (total_in / count)*100;

    System.out.println("\nSummary:");
    System.out.println("Best Game Free Throws Made: " + maxShots);
    System.out.println("Worst Game Free Throws Made: " + minShots);
    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!");     
 }
}

Do it as follows:

import java.util.Scanner;

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;
        int worst = Integer.MAX_VALUE, best = Integer.MIN_VALUE;

        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 ");
                }
            }
            worst = Math.min(worst, in);
            best = Math.max(best, in);
            System.out.println("\nFree Throws Made: " + 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: " + best);
        System.out.println("Worst Game Free Throws Made: " + worst);
        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!");
    }
}

A test run:

Enter Player's Free Throw Percentage: 60

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

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

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

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

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

Summary:
Best Game Free Throws Made: 7
Worst Game Free Throws Made: 5
Total Free Throws Made: 29 Out Of 50
Average Free Throw Percentage: 58%

END OF SIMULATION!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM