首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试多次循环for循环

尝试多次循环for循环
EN

Stack Overflow用户
提问于 2018-03-06 06:48:49
回答 1查看 195关注 0票数 0

希望这个问题是有意义的,但本质上这是我遇到的问题。我的任务是创建一个程序,将某人的罚球命中率作为输入,然后模拟5场比赛,他们试图在每场比赛中投10次罚球。以及之后的总结,如最佳比赛,最差比赛,所有比赛的总得分,以及平均罚球命中率。

到目前为止,我试图让我的模拟游戏多次运行,但似乎找不到答案。这就是我目前所拥有的: import java.util.*;

代码语言:javascript
复制
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,任何关于我可能做错了什么的见解,或者关于如何让它做我想做的事情的建议,我都会非常感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-06 07:10:10

我已经添加到你的代码中,所以现在有一个百分比和一个总数,你几乎是正确的,只需要做一个小的改变,这样计数就可以被携带和显示。如果你想有一个最好/最差的游戏,你需要创建两个新的变量,并在阈值为1)最佳游戏和2)更低的最差游戏时为每个游戏更新它们。

如果你遇到这个问题,告诉我,我会帮你的。对你来说,用你目前所知道的实现起来应该很容易。

问题是你在没有必要的情况下返回i。在这里已经消失了:

代码语言:javascript
复制
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);

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49120780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档