希望这个问题有意义,但基本上这就是我遇到的问题。我的任务是创建一个程序,以某人的罚球投篮命中率为输入,然后模拟5场比赛,他们试图每场比赛投10次罚球。此外还有一个总结,然后显示最好的游戏,最差的游戏,所有游戏的总和,平均罚球百分比。
我目前已经达到了这样的程度,我试图让我的模拟游戏多次运行,但似乎无法弄明白。这是我到目前为止:import java.util。*;
public class FreeThrow {
public static int simulate(int input){
int i;
int j;
int count = 0;
for (j = 1; j < 6; j++){
System.out.println("Game " + j + ":");
for(i = 0;i < 10; i++){
int shot = (int)(Math.random()*101)-1;
if (shot > input){
System.out.print("OUT ");//shot missed
} else {
System.out.print("IN ");//shot made
count++;
}
}
//prints the number of free throws made per game out of 10
System.out.print("\nFree throws made: " + count + " out of 10.");
return i;
}
return j;
}
public static void main (String[] args){
//asks for user input to detemine player free throw percentage
Scanner scan = new Scanner(System.in);
System.out.print("Enter Player's Free Throw Percentage: ");
int input = scan.nextInt();
simulate(input);
}
}
正如您所看到的,我目前在for循环中有一个for循环。我这样做是为了尝试再次模拟游戏循环循环,同时添加上面显示的“游戏1:”行并显示每个游戏。它只适用于一款游戏。我看起来完全像它应该。以下是教授希望它看起来像的一个例子:Link To Image任何关于我可能做错了什么的见解或关于如何让它做我想做的事情的建议将不胜感激。
我已经添加到你的代码中,所以现在有一个百分比和一个总数,你几乎是正确的,只需做一个小的改变,以便可以携带和显示计数。如果你想拥有一个最好/最差的游戏,你需要创建两个新的变量,并且如果阈值是1)为最佳游戏打败,则为每个游戏更新它们,以及2)对于最差游戏更低。
如果你遇到这个让我知道,我会帮你一把。应该很容易实现您目前所知道的。
问题是你在没有必要时回来了。这一点已被证实:
public class freeThrow {
private static int count;
public static int simulate(int input){
int i;
int j;
for (j = 1; j < 6; j++){
System.out.println("Game " + j + ":");
for(i = 0;i < 10; i++){
int shot = (int)(Math.random()*101)-1;
if (shot > input){
System.out.print("OUT ");//shot missed
} else {
System.out.print("IN ");//shot made
count++;
}
}
//prints the number of free throws made per game out of 10
System.out.println("\nFree throws made: " + count + " out of 10.");
}
return j;
}
public static int average(int count) {
int average = count/5;
System.out.println("\nAverage is " + average*10 + "%");
return average;
}
public static int totalShots(int count) {
int total = count;
System.out.println("total shots made " + total);
return total;
}
public static void main (String[] args){
//asks for user input to detemine player free throw percentage
Scanner scan = new Scanner(System.in);
System.out.print("Enter Player's Free Throw Percentage: ");
int input = scan.nextInt();
simulate(input);
average(count);
totalShots(count);
}
}