输出指定数字随意组合 算法二 (面向对象的思路)(C#版本)

using System.Linq;

namespace Common
{
	public class DigitalDashBoard
	{
		private static string RANGE = "0123456789";
		private char[] chars;

		public DigitalDashBoard(int length)
		{
			// 仪表盘显示位数
			this.chars = new char[length];
			// 初始化
			for (int i = 0; i < chars.Length; i++)
			{
				chars[i] = RANGE.ElementAt(0);
			}
		}

		/// <summary>
		/// 累加
		/// </summary>
		/// <returns>是否操作成功</returns>
		public bool Add()
		{
			// 从最后一位开始累加
			for (int i = chars.Length - 1; i >= 0; i--)
			{
				// 累加
				int index = RANGE.IndexOf(chars[i]);
				if (index < (RANGE.Length - 1))
				{
					chars[i] = RANGE.ElementAt(index + 1);
					// 不溢出则跳出循环
					break;
				}
				chars[i] = RANGE.ElementAt(0);
			}
			// 判断是否最大值
			for (int i = 0; i < chars.Length; i++)
			{
				if (chars[i] != RANGE.Last())
				{
					return true;
				}
			}
			return false;
		}

		/// <summary>
		/// 获取值
		/// </summary>
		/// <returns></returns>
		public string GetValue()
		{
			return new string(chars);
		}
	}
}

 

posted on 2020-03-17 14:14  小小程序员的梦想  阅读(332)  评论(0编辑  收藏  举报

导航