如何构建一个类?在c#console应用程序中 [英] how to construct a class? in c# console application

查看:72
本文介绍了如何构建一个类?在c#console应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对课程感到困惑..如何将这个代码构造成类?在C#console appli

  static   void  Main( string  [] args)
{

Random generator = new Random();
int Random = generator.Next( 1032 2353 );

Console.WriteLine(随机);
}
}







--------- ------------------------------------

 静态  void  Main( string  [ ] args)
{
Console.WriteLine( RandomCode:);
string code = string .Intern(Console.ReadLine());

随机生成器= new Random();
generator.Code = code;
}


// 我的班级

class 随机
{
public Random() {}


private string 代码;
public string 代码
{
获取 {返回代码; }
set {code = value ; }
}

}



我不知道怎么把它放到课堂上。

< pre lang =c#> Random generator = new Random();
int Random = generator.Next( 1032 2353 );

Console.WriteLine(随机);



-------

解决方案

 使用系统; 

public static class Randomizer
{
private static 随机RND
{
get { return _rnd ?? (_rnd = new Random()); }
}
private static Random _rnd;

public static int GetNext()
{
return RND.Next();
}

public static int GetNext( int max)
{
return RND.Next(max);
}

public static int GetNext( int min, int max)
{
return RND.Next(min,max);
}
}

class 计划
{
static void Main( string [] args)
{
Console.WriteLine( 下一个随机是:{0},Randomizer.GetNext(< span class =code-digit> 1032 , 2353 ));
Console.WriteLine( 下一个随机是:{0},Randomizer.GetNext( 1032 2353 ));
Console.WriteLine( 下一个随机是:{0},Randomizer.GetNext( 1032 2353 ));
Console.ReadKey();
return ;


i confuse about class.. how to construct this code to class? in C#console appli

static void Main(string[] args)
{

Random generator = new Random();
int Random = generator.Next(1032, 2353);

Console.WriteLine(Random);
}
}




---------------------------------------------

static void Main(string[] args)
{
          Console.WriteLine("RandomCode:");
          string code = string.Intern (Console.ReadLine());

          Random generator = new Random();
          generator.Code = code;
}


//my class

class Random
{
    public Random() { }


    private string code;
    public string Code
    {
        get { return code; }
        set { code = value; }
    }

}


I dont know how to put this to a class.

Random generator = new Random();
int Random = generator.Next(1032, 2353);

Console.WriteLine(Random);


-------

解决方案

using System;

public static class Randomizer
   {
       private static Random RND
       {
           get { return _rnd ?? (_rnd = new Random()); }
       }
       private static Random _rnd ;

       public static int GetNext()
       {
           return RND.Next();
       }

       public static int GetNext(int max)
       {
           return RND.Next(max);
       }

       public static int GetNext(int min, int max)
       {
           return RND.Next(min, max);
       }
   }

   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("Next random is: {0}", Randomizer.GetNext(1032, 2353));
           Console.WriteLine("Next random is: {0}", Randomizer.GetNext(1032, 2353));
           Console.WriteLine("Next random is: {0}", Randomizer.GetNext(1032, 2353));
           Console.ReadKey();
           return;


这篇关于如何构建一个类?在c#console应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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