希望这个问题是有意义的,但本质上这是我遇到的问题。我的任务是创建一个程序,将某人的罚球命中率作为输入,然后模拟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循环。我这样做是为了尝试让模拟的游戏循环再次循环,同时添加上面显示的"Game 1:“行,并显示每个游戏是什么。它只在一场比赛中完美发挥作用。我让它看起来和应该的一模一样。还有,这里有一个教授希望它看起来是什么样子的例子:Link To Image,任何关于我可能做错了什么的见解,或者关于如何让它做我想做的事情的建议,我都会非常感激。
发布于 2018-03-06 07:10:10
我已经添加到你的代码中,所以现在有一个百分比和一个总数,你几乎是正确的,只需要做一个小的改变,这样计数就可以被携带和显示。如果你想有一个最好/最差的游戏,你需要创建两个新的变量,并在阈值为1)最佳游戏和2)更低的最差游戏时为每个游戏更新它们。
如果你遇到这个问题,告诉我,我会帮你的。对你来说,用你目前所知道的实现起来应该很容易。
问题是你在没有必要的情况下返回i。在这里已经消失了:
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);
}
}https://stackoverflow.com/questions/49120780
复制相似问题