随机整数数组中每个值的频率

发布于 2025-01-21 12:57:49 字数 2021 浏览 1 评论 0原文

需要帮助,在我必须在30球中生成1到6之间的随机跑步并获得: 1.总数得分 2. 0s,1s,2s,3s,4s和6s的数量 3.

在我获得“总跑步”和“打击率”时,我无法获得0s,1s ...的频率... 我尝试使用计数器和流方法,但似乎无法正确处理。 高度赞赏您的帮助。 谢谢你!

这是实际的代码,我现在将频率部分标记为块,以便至少其他方法执行...

import java.util.Random;
public class Assignment_2 {
    
    public static void main(String[] args) {
        Random r = new Random();
        System.out.println("Runs for 30 balls");
        int ball[] = new int[30];
        for(int i=0; i<ball.length; i++)
        {
            ball[i] = r.nextInt(6);
            System.out.print(ball[i]+" , ");**
         
    /*  int zeros = 0;
        int ones = 0;
        int twos = 0;
        int threes = 0;
        int fours = 0;
        int fives = 0;
        int sixes = 0;
            if (r.nextInt() == 0 ) {
                zeros++;
            } 
            else if (r.nextInt() == 1) {
                ones++;
            } 
            else if (r.nextInt() == 2) {
                twos++;
            }
            else if (r.nextInt() == 3) {
                threes++;
            }
            else if (r.nextInt()== 4) {
                fours++;
            }
            else if (r.nextInt() == 5) {
                fives++;
            }
            else if (r.nextInt() == 6) {
                sixes++;
            }
            System.out.println(zeros);
            System.out.println(ones);
            System.out.println(twos);
            System.out.println(threes);
            System.out.println(fours);
            System.out.println(fives);
            System.out.println(sixes);
    */
        **}
        
        System.out.println();
        
        System.out.println("Runs Scored");
        float TR=0;
        for(int i : ball)
        {
            TR += i;
        }  
        System.out.print(TR);
        
        System.out.println();
        
        System.out.println("Strike Rate");
        float SR=(TR/30)*100;
        System.out.print(SR);
        
        System.out.println();
        
        
    }
}

Need help with an assignment where I have to generate random runs between 1 to 6 for 30 balls and get :
1.Total runs scored
2.Number of 0s, 1s, 2s, 3s, 4s and 6s
3.Strike Rate

While I have got 'Total runs' and 'Strike rate', I am unable to get the frequency of 0s,1s...
I have tried using counter and stream method, but can't seem to get it right.
Your help is highly appreciated.
Thank you!

And here's the actual code, I have marked the frequency part as block for now so that atleast other methods execute...

import java.util.Random;
public class Assignment_2 {
    
    public static void main(String[] args) {
        Random r = new Random();
        System.out.println("Runs for 30 balls");
        int ball[] = new int[30];
        for(int i=0; i<ball.length; i++)
        {
            ball[i] = r.nextInt(6);
            System.out.print(ball[i]+" , ");**
         
    /*  int zeros = 0;
        int ones = 0;
        int twos = 0;
        int threes = 0;
        int fours = 0;
        int fives = 0;
        int sixes = 0;
            if (r.nextInt() == 0 ) {
                zeros++;
            } 
            else if (r.nextInt() == 1) {
                ones++;
            } 
            else if (r.nextInt() == 2) {
                twos++;
            }
            else if (r.nextInt() == 3) {
                threes++;
            }
            else if (r.nextInt()== 4) {
                fours++;
            }
            else if (r.nextInt() == 5) {
                fives++;
            }
            else if (r.nextInt() == 6) {
                sixes++;
            }
            System.out.println(zeros);
            System.out.println(ones);
            System.out.println(twos);
            System.out.println(threes);
            System.out.println(fours);
            System.out.println(fives);
            System.out.println(sixes);
    */
        **}
        
        System.out.println();
        
        System.out.println("Runs Scored");
        float TR=0;
        for(int i : ball)
        {
            TR += i;
        }  
        System.out.print(TR);
        
        System.out.println();
        
        System.out.println("Strike Rate");
        float SR=(TR/30)*100;
        System.out.print(SR);
        
        System.out.println();
        
        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2025-01-28 12:57:49
 if (r.nextInt() == 0 )

ETC比较了新生成的随机数。 You want to compare what is already used for the ball:

if ( ball[i] == 0 ) .. etc. although using an array to store the counts instead of individual variables would be cleaner and less代码。

 if (r.nextInt() == 0 )

etc is comparing a newly generated random number. You want to compare what is already used for the ball:

if ( ball[i] == 0 ) .. etc. although using an array to store the counts instead of individual variables would be cleaner and less code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文