模拟篮球罚球表现和百分比。代码工作正常,但是在最后的打印摘要中,我需要报告5个游戏中的最高得分和最低得分。输出只需要说“最佳游戏罚球次数:” +一个数字(最大值)。然后,我也需要同样的东西来获得最差的游戏分数(分钟)。我想我需要从for循环结果中创建一个数组,但是在如何获得类似功能方面迷茫了。
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!");
}
}
问题来源:Stack Overflow
如下进行:
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!");
}
}```
测试运行:
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!
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。