有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java分离循环每次迭代的结果

我的任务是模拟一名篮球罚球运动员在5场比赛中投篮命中率,每场10次。在大多数情况下,代码都是按照我希望的方式工作的,但是对于每一个游戏,我需要打印出10张中的in张。我不知道如何保持这个结果特定于相关的game,而不是在所有游戏中增加in数。接下来的每一个游戏都将其in金额添加到前一个游戏中

然后我还需要帮助,根据每场比赛的in投篮次数,找出最佳和最差得分。我相信这两个问题可以通过同样的行动来解决,我只是很难找到需要采取的行动

import java.util.*;

public class Final2 {
    public static void main(String[] args){
        double in;
        int out;
        int count;
        int games;
        int tries;
        double average;
        int total;

        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Player's Free Throw Percentage: ");
        int input = scan.nextInt();
        count = 0;
        in = 0;
        average = 0;
        total = 0;
        games = 1;

        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: " + String.format("%.0f", in) + " Out Of 10. ");
        }   
        while (games <= 5);{
        }           
        average = (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", 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: 50

Game 1:
IN IN IN IN IN IN IN OUT OUT OUT 
Free Throws Made: 7 Out Of 10. 

Game 2:
OUT IN OUT OUT OUT IN OUT IN IN IN 
Free Throws Made: 12 Out Of 10. 

Game 3:
IN OUT IN IN IN OUT OUT OUT OUT OUT 
Free Throws Made: 16 Out Of 10. 

Game 4:
IN OUT OUT OUT IN OUT OUT OUT IN IN 
Free Throws Made: 20 Out Of 10. 

Game 5:
OUT OUT IN OUT OUT OUT IN IN IN IN 
Free Throws Made: 25 Out Of 10. 

Summary:
Best Game Free Throws Made: ...
Worst Game Free Throws Made: ...
Total Free Throws Made: 25 Out Of 50
Average Free Throw Percentage: 50%

END OF SIMULATION!

共 (1) 个答案

  1. # 1 楼答案

    您必须在while之前重置in变量。如果您想跟踪in的总数,必须使用一个新变量(例如total_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 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: " + String.format("%.0f", 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!");     
        }
    }