C#练习题答案: 预FizzBu​​zz锻炼#2【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

预FizzBu​​zz锻炼#2【难度:1级】:

答案1:

public class Kata
{
  public static string PreFizz(int n)
  {
    return n%3==0 && n%5==0 ? "FizzBuzz": n%3==0 ? "Fizz":n%5==0? "Buzz": n.ToString();
  }
}

答案2:

public class Kata
{
  public static string PreFizz(int n)
  {
    return (n % 3 == 0 && n % 5 == 0) == true ? "FizzBuzz" : n % 3 == 0 ? "Fizz" : n % 5 == 0 ? "Buzz" : n.ToString();;
  }
}

答案3:

public class Kata
{
   public static string PreFizz(int n)
        {

            
                bool fizz = n%3 == 0;
                bool buzz = n%5 == 0;
                if (fizz && buzz)
                    return "FizzBuzz";
                else if (fizz)
                    return ("Fizz");
                else if (buzz)
                    return("Buzz");
                else
                return n.ToString();


        }
}

答案4:

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

public class Kata
{
  static Dictionary<int, string> dic = new Dictionary<int, string> { { 3, "Fizz" }, { 5, "Buzz" } };
  
  public static string PreFizz(int n)
  {
      var s = string.Concat(dic.Select(kvp => n % kvp.Key == 0 ? kvp.Value : ""));
      return s != "" ? s : n.ToString();
  }
}

答案5:

public class Kata
{
  public static string PreFizz(int n)
  {
    return (n % 5 == 0) ? (n % 3 == 0 ? "FizzBuzz" : "Buzz") : (n % 3 == 0 ? "Fizz" : n.ToString());
  }
}

答案6:

public class Kata
{
  public static string PreFizz(int n)
  {
    string res = "";
    if (n % 3 == 0) res += "Fizz";
    if (n % 5 == 0) res += "Buzz";
  
    return string.IsNullOrEmpty(res) ? "" + n : res;
  }
}

答案7:

public class Kata
{
  public static string PreFizz(int n)
  {
    switch(n % 3)
    {
      case 0:
      {
        switch (n % 5)
        {
          case 0:
          {
            return "FizzBuzz";
          }
        }
        return "Fizz";
       }
     default:
       switch (n % 5)
       {
          case 0:
            {
              return "Buzz";
            }
        }
    return n.ToString();
    }
  }
}

答案8:

public class Kata
{
        public static string PreFizz(int n)
        {
            if (n % 5 == 0)
            {
                if (n% 3 == 0)
                {
                    return "FizzBuzz";
                }
                else
                {
                    return "Buzz";
                }
            }
            return n % 3 == 0 ? "Fizz" : n.ToString();
        }
}

答案9:

public class Kata
{
  public static string PreFizz(int n) => n % 3 == 0 &amp;&amp; n % 5 == 0 ? "FizzBuzz" : n % 3 == 0 ? "Fizz" : n % 5 == 0 ? "Buzz" : n.ToString();  
}

答案10:

    public class Kata
    {
        public static string PreFizz(int n) => n % 5 == 0 &amp;&amp; n % 3 == 0 ? "FizzBuzz" :
        n % 5 == 0 ? "Buzz" : n % 3 == 0 ? "Fizz" : n.ToString();
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值