C#练习题答案: 颜色鬼【难度:0级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

颜色鬼【难度:0级】:

答案1:

using System;
using System.Collections.Generic;

public class Ghost
{
  private static Random _rnd = new Random();
  private static List<String> _colors = new List<String>{"white", "yellow", "purple", "red"};
  public String GetColor(){
    return _colors[_rnd.Next(_colors.Count)];
  }
}

答案2:

using System;

public class Ghost
{
  private readonly string[] Colors = {"white", "yellow", "purple", "red"};
  public string GetColor() { return Colors[new Random().Next(0, 4)]; }
}

答案3:

using System;

public class Ghost
{
  private static string[] colors = {"white", "yellow", "purple", "red"};
  private string color;
  
  public Ghost()
  {
    color = colors[new Random().Next(colors.Length)];
  }
  
  public string GetColor()
  {
    return color;
  }
}

答案4:

using System;

public class Ghost
{
    private string[] array = { "white", "yellow", "purple", "red" };
    public string result = "";

    Random _random = new Random();

    public Ghost()
    {
        result = array[_random.Next(array.Length)];
    }

    public string GetColor()
    {
        return result;
    }
}

答案5:

using System;

public class Ghost
{
    private static readonly string[] colors = new string[] { "white", "yellow", "purple", "red" };
    private string color = colors[new Random().Next(colors.Length)];
    public string GetColor()
    {
        return color;
    }
}

答案6:

using System;

public class Ghost
{
  private string color;
  
  public string GetColor()
  {
      return color ?? (color = new []{"white", "yellow", "purple", "red"}[(new Random()).Next(0, 4)]);
  }
}

答案7:

using System;

public class Ghost
{
  private static readonly string[] colors = new [] {"white", "yellow", "purple", "red"};
  
  private readonly string color;
  
  public Ghost()
  {
    var random = new Random();
    color = colors[random.Next(4)];
  }
  
  public string GetColor()
  {
    return color;
  }
}

答案8:

using System;

public class Ghost
{
  private readonly string _color;
  
  public Ghost()
  {
    switch(this.GetHashCode() % 4)
    {
      case 0:
        _color = "white";
        break;
      case 1:
        _color = "yellow";
        break;
      case 2:
        _color = "purple";
        break;
      case 3:
        _color = "red";
        break;
    }
  }

  public string GetColor() {
    return _color;
  }
}

答案9:

using System;

public class Ghost
{
    private static readonly string[] colors = { "white", "purple", "red", "yellow" };    
    
    public string GetColor() 
    {
        return colors[DateTime.Now.Millisecond % colors.Length];
    }
}

答案10:

using System;

public class Ghost
{
  public string GetColor()
  {
    return new string[]{"white", "yellow", "purple", "red"}[new Random().Next(4)];
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值