C# 掷骰子法

C# 掷骰子法,c#,C#,我需要两种方法。第一个方法抛出四个骰子并返回骰子总数。第二种方法估计总和,如果总和大于20,则打印“优秀”;如果总和大于12或总和小于或等于20,则打印“良好”;如果总和小于或等于12,则打印“差” 骰子是通过写行掷来的 Random ran = new Random (); int throwingdice = ran.nextInt(1,7); 我试了很多次,但都没用,有什么想法吗? 提前感谢您。这将帮助您开始 using System; using System.Collections.

我需要两种方法。第一个方法抛出四个骰子并返回骰子总数。第二种方法估计总和,如果总和大于20,则打印“优秀”;如果总和大于12或总和小于或等于20,则打印“良好”;如果总和小于或等于12,则打印“差”

骰子是通过写行掷来的

Random ran = new Random ();
int throwingdice = ran.nextInt(1,7);
我试了很多次,但都没用,有什么想法吗?
提前感谢您。

这将帮助您开始

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Random r = new Random();

            int sum=0;
            for (int i = 0; i < 4; i++)
            {
                var roll = r.Next(1, 7);
                sum += roll;
            }
            // sum should be the sum of the four dices
            Console.WriteLine("the sum of the first 4 throws is {0}", sum);

            if (sum > 20)
            {
                Console.WriteLine("place your message in here stating that sum gas greater than 20");
            }
            else if (sum < 10)
            {
                Console.WriteLine("sum is less than 10");
            }
            else
            {
                Console.WriteLine("some other message");
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
随机r=新随机();
整数和=0;
对于(int i=0;i<4;i++)
{
var roll=r.Next(1,7);
总和+=滚动;
}
//总和应该是四个骰子的总和
WriteLine(“前4次抛出的总和为{0}”,sum);
如果(总和>20)
{
Console.WriteLine(“将您的消息放在此处,说明气体总量大于20”);
}
否则,如果(总和<10)
{
控制台写入线(“总和小于10”);
}
其他的
{
Console.WriteLine(“其他消息”);
}
}
}
}
  • 您应该重命名变量“throw”,因为throw在C#中有特殊的含义,比如for、each、int等

  • 下面是使用random()的示例:

    资料来源:


  • 好的,那么第二种方法的目的是什么,当您想要的一切都可以通过一种方法实现,如Tono Nam所示。只需添加一个switch语句或if-else块即可输出字符串值

    public void yourMethod()
    {
        Random r = new Random(); 
    
        int sum=0;       
    
        for (int i = 0; i < 4; i++)         
        {             
            var roll = r.Next(1, 7);             
            sum += roll;         
        }         // sum should be the sum of the four dices 
    
    
        //a switch is faster for a larger amount of options
        switch(sum)
        {
            case 20:
            {
                Console.WriteLine("excellent");
            }
        }
    
        //Or use If else
        if(sum>=20)
        {
            Console.WriteLine("excellent");
        }
    }
    
    public方法()
    {
    随机r=新随机();
    整数和=0;
    对于(int i=0;i<4;i++)
    {             
    var roll=r.Next(1,7);
    总和+=滚动;
    }//sum应该是四个骰子的和
    //对于更多的选项,切换速度更快
    开关(总和)
    {
    案例20:
    {
    Console.WriteLine(“优秀”);
    }
    }
    //或者如果有其他的话就使用
    如果(总和>=20)
    {
    Console.WriteLine(“优秀”);
    }
    }
    

    这只是一个例子,必须建立起来。

    您尝试了什么?作业旗?帮你做家庭作业没有问题,但至少试一下你遇到的确切问题是什么?你应该明确哪些不起作用。你必须掷骰子和读骰子的硬件是什么?我不是在学校,我只想学习c#
    掷骰子是一个关键字,你需要另一个变量名开始当你编辑代码时,你应该看到顶部的帮助。或者你可以缩进你的代码来格式化它…我已经试过了。。。队形完全搞乱了,很奇怪,然后确保缩进足够多。在每行之前放置大约20个空格就可以了。如果你缩进不够,它就不起作用了。在stackoverflow的编辑器上通过它之后,从visual studio或ide复制代码,选择它并单击文本区域编辑器顶部的{}按钮…谢谢,我不得不添加比通常的4个空格多得多的代码,然后它就工作了!:)非常感谢,这是我需要的。
    
    public void yourMethod()
    {
        Random r = new Random(); 
    
        int sum=0;       
    
        for (int i = 0; i < 4; i++)         
        {             
            var roll = r.Next(1, 7);             
            sum += roll;         
        }         // sum should be the sum of the four dices 
    
    
        //a switch is faster for a larger amount of options
        switch(sum)
        {
            case 20:
            {
                Console.WriteLine("excellent");
            }
        }
    
        //Or use If else
        if(sum>=20)
        {
            Console.WriteLine("excellent");
        }
    }