【问题标题】:Min & Max results of a for loop in javajava中for循环的最小和最大结果
【发布时间】:2020-03-12 22:41:45
【问题描述】:

模拟篮球罚球表现和百分比。代码运行良好,但在最后的打印摘要中,我需要报告 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!");     
 }
}

【问题讨论】:

    标签: java arrays loops


    【解决方案1】:

    不要想得那么复杂,作为不解决赋值的帮助:考虑为最小值和最大值使用变量。

    【讨论】:

    • 我相信你对我的回答投了反对票。请让我知道您希望在答案中看到什么改进。如果不小心,请取消。
    • 哦...好的...谢谢您的回复。
    【解决方案2】:

    每次在游戏循环中检查 in 是否大于您的“跑步最大值”(即 maxShots),是否 in 小于您的“跑步最小值”(即 minShots)。

    还要注意如何初始化这些最大和最小变量。如果设置 minShots = 0,它可能会报告最小值为 0,即使实际最小值大于该值。这是因为当您检查 in 是否小于 minShots 时,0 将始终小于或等于 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 maxShots = 0;
        int minShots = 10;
        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;
                if(in > maxShots){
                    maxShots = in;
                }
                if(in < minShots){
                    minShots = in;
                }
                in = 0;
            }   
            while (games <= 5);{
            }           
            average = (total_in / count)*100;
    
        System.out.println("\nSummary:");
        System.out.println("Best Game Free Throws Made: " + maxShots);
        System.out.println("Worst Game Free Throws Made: " + minShots);
        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!");     
     }
    }
    

    【讨论】:

      【解决方案3】:

      按如下方式进行:

      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!
      

      【讨论】:

      • 这真是完美!谢谢你的洞察力。我正在考虑让我陷入困境的解决方案变得复杂。再次感谢您的帮助!绝对学会了退后几步并简化方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 2021-11-27
      • 2015-09-04
      • 1970-01-01
      相关资源
      最近更新 更多