开发者社区> 问答> 正文

Java中for循环的最小和最大结果

模拟篮球罚球表现和百分比。代码工作正常,但是在最后的打印摘要中,我需要报告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

展开
收起
montos 2020-03-27 17:41:32 479 0
1 条回答
写回答
取消 提交回答
  • 如下进行:

    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
    2020-03-27 17:42:01
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载