如何打印if语句中使用的随机数? [英] How do I print a random that is used in a if statement?

查看:95
本文介绍了如何打印if语句中使用的随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1:这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            //variables
            double NumOfSixes = 0;
            Random rand = new Random();

            //loop for dices
            for (int i=0 ; i<100000 ; i++)
            {
                //get number of sixes 
                if (rand.Next(1,7) == 6)
                {
                    NumOfSixes++;
                }
            }

            Console.WriteLine(NumOfSixes);
            Console.WriteLine(NumOfSixes / 100000);
            Console.ReadKey();
        }
    }
}

这只是我练习它的一个非常简单的程序,但我遇到的问题是我无法打印我的数字。现在所有的节目输出都是六次投掷的概率是十进制形式,我得到了多少六个。但是我想要程序打印它生成的每个数字。所以输出看起来有点像这样:

This is just a very simple program that I made to practise it works, but the problem Im having is that I cant print my numbers. Right now all the program outputs is the probability of a six being thrown is in decimal form and how many sixes I got. But I want the program to print every number that it generates. So that the output would look a bit like this:

1

3

5

6

7 ....

numofsixes

numofsixes

概率

我对如何做到这一点的想法是:

My idea of how to do this was:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            //variables
            double NumOfSixes = 0;
            Random rand = new Random();

            //loop for dices
            for (int i=0 ; i<100000 ; i++)
            {
               

               Console.WriteLine(rand.Next(1,7));

                //get number of sixes 

                 if (rand == 2)
                {
                    NumOfSixes++;
                }
            }

            Console.WriteLine(NumOfSixes);
            Console.WriteLine(NumOfSixes / 100000);
            Console.ReadKey();
        }
    }
}


但是当我这样做时,它无法计算出任何整数随机对象。因此if语句是一个很大的错误。

But when I do this it is unable to make out any integers out of a Random object. Thus the if statement is a big old error.

推荐答案

    class Program
    {
        static void Main(string[] args)
        {
            //variables
            double NumOfSixes = 0;
            Random rand = new Random();

            //loop for dices
            for (int i = 0; i < 100000; i++)
            {

                var randomInt = rand.Next(1, 7);
                Console.WriteLine(randomInt);

                //get number of sixes 

                if (randomInt == 6)
                {
                    NumOfSixes++;
                }
            }

            Console.WriteLine(NumOfSixes);
            Console.WriteLine(NumOfSixes / 100000);
            Console.ReadKey();
        }
    }


这是你想要的吗?

Is this what you want?


这篇关于如何打印if语句中使用的随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆