JusLip :
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!");
}
}
Arvind Kumar Avinash :
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!
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=238309&siteId=1