我有一个任务是模拟5次比赛中篮球罚球命中率,每场10次。在大多数情况下,代码可以按照我的意愿进行工作,但对于每个游戏,我都需要打印出10张“进”球的数量。我无法弄清楚如何使此结果特定于相关的“游戏”而不是增加所有游戏的“入场”数量,随后的每个游戏都将其上一场游戏的“入场”数量相加。
然后,我还需要帮助,根据每个游戏的“中”投篮次数确定最佳和最差得分。我相信可以通过相同的动作来解决这两个问题,但是我只是在寻找需要采取的动作上遇到麻烦。
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!");
}
}
输出:
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!
您必须在片刻之前重设in变量。如果要跟踪in的总数,则必须使用新变量(例如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 += (Double) 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!");
}
}